xcode 限制 UITableView 的滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12805615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limit the scroll for UITableView
提问by Ali
I have a TableViewController:
我有一个 TableViewController:
As you see I have my own custom bar at the top.
The UITable View is just a static
one, and I add a view at the top of UITableView.
如您所见,我在顶部有自己的自定义栏。UITable View 只是static
一个,我在 UITableView 的顶部添加了一个视图。
The thing is when I scroll the TableView to top-side it become like bellow image, and I don't want it. is there any easy code that I can limit the scroll for the tableView?
问题是当我将 TableView 滚动到顶部时,它变得像波纹管图像,我不想要它。是否有任何简单的代码可以限制 tableView 的滚动?
回答by Matthias Bauch
since UITableView is a subclass of UIScrollView you can use this UIScrollViewDelegate method to forbid scrolling above the top border
由于 UITableView 是 UIScrollView 的子类,因此您可以使用此 UIScrollViewDelegate 方法来禁止在顶部边框上方滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.tableView) {
if (scrollView.contentOffset.y < 0) {
scrollView.contentOffset = CGPointZero;
}
}
}
回答by Omar Abdelhafith
Yo will need to set the bounce property of the uitableview to NO
你需要将 uitableview 的反弹属性设置为 NO
UITableView *tableView;
tableView.bounces = NO;
Edit: Note also you can uncheck the bounces from interface builder too
编辑:请注意,您也可以取消选中界面构建器中的反弹
Please check this answer for further details Disable UITableView vertical bounces when scrolling
请检查此答案以获取更多详细信息滚动时禁用 UITableView 垂直反弹
回答by ramzesenok
I had the same problem and asked our UX-Designer, how it would be better to do. He said, that both strict solutions (prevent bouncing or allow it as it is) are bad. It's better to allow bouncing but only for some space
我遇到了同样的问题,并问我们的 UX 设计师,怎么做会更好。他说,这两种严格的解决方案(防止弹跳或按原样允许)都是不好的。最好允许弹跳但仅限于某些空间
My solution was:
我的解决方案是:
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.tableView {
if scrollView.contentOffset.y < -64 {
scrollView.scrollRectToVisible(CGRect(origin: CGPoint(x: 0, y: -64), size: scrollView.frame.size), animated: false)
scrollView.scrollRectToVisible(CGRect(origin: CGPoint.zero, size: scrollView.frame.size), animated: true)
}
}
}
Where 64
was that "some space" for me. Code stops tableView at -64
from the top and brings it up with an animation.
Good luck!
64
对我来说,那个“一些空间”在哪里?代码-64
从顶部停止 tableView并用动画显示它。祝你好运!
回答by tiguero
If i understand correctly you have set-up your custom bar as part of your tableview. Put your custom bar
in a separate view not in the tableview and put your tableview below custom bar when you are setting up your views. You need to create your custom view controller that will have your custom bar and your static table view.
如果我理解正确,您已将自定义栏设置为 tableview 的一部分。将您custom bar
的视图放在一个单独的视图中,而不是在 tableview 中,并在设置视图时将 tableview 放在自定义栏下方。您需要创建具有自定义栏和静态表视图的自定义视图控制器。
回答by iDev
You need to create your view controller object as type UIViewController
and not UITableViewController
. Then add the custom bar as a subview to self.view
. Create a separate UITableView
and add it below the custom bar. That should make custom bar static and table view scrollable.
您需要将视图控制器对象创建为 typeUIViewController
而不是UITableViewController
。然后将自定义栏作为子视图添加到self.view
. 创建一个单独的UITableView
并将其添加到自定义栏下方。这应该使自定义栏静态和表格视图可滚动。
Update:
更新:
In order to make the tableview static you need to set it as
为了使 tableview 静态,您需要将其设置为
tableView.scrollEnabled = NO:
Let me know if this works for you.
让我知道这是否适合您。