xcode 判断 UITableView 是否已经滚动到顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15772859/
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
Tell if UITableView has scrolled to top
提问by JoshDG
I tried this:
我试过这个:
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
But it didn't fire when I scrolled the table view to the top.
但是当我将表格视图滚动到顶部时它没有触发。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
Does fire so the delegate isn't the problem.
是否触发,因此代表不是问题。
In viewDidLoad I also set [myTbl setDoesScrollToTop:YES];
在 viewDidLoad 我也设置 [myTbl setDoesScrollToTop:YES];
回答by Elliott James Perry
The scrollViewDidScrollToTop:
method fires when the user clicks on the status bar and the scrollsToTop
property is set to YES
. From the docs:
scrollViewDidScrollToTop:
当用户单击状态栏并且scrollsToTop
属性设置为时,将触发该方法YES
。从文档:
The scroll view sends this message when it finishes scrolling to the top of the content. It might call it immediately if the top of the content is already shown. For the scroll-to-top gesture (a tap on the status bar) to be effective, the scrollsToTop property of the UIScrollView must be set to YES.
当滚动视图完成滚动到内容的顶部时,它会发送此消息。如果内容的顶部已经显示,它可能会立即调用它。要使滚动到顶部手势(状态栏上的点击)有效,UIScrollView 的 scrollsToTop 属性必须设置为 YES。
It does not fire if the user manually scrolls to the top. If you want to handle this case you will have to implement the scrollViewDidScroll:
method and check to see whether the scroll is at the top yourself.
如果用户手动滚动到顶部,它不会触发。如果您想处理这种情况,您将必须实现该scrollViewDidScroll:
方法并自己检查滚动条是否位于顶部。
You can check this through the contentOffsetproperty e.g.:
您可以通过contentOffset属性检查这一点,例如:
if (scrollView.contentOffset.y == 0) { // TOP }
回答by naydin
When the table view goes under navigation bar and safe area layout guides are enabled, the following check can be done:
当表格视图进入导航栏并启用安全区域布局指南时,可以进行以下检查:
if (tableView.contentOffset.y + tableView.safeAreaInsets.top) == 0 { ... }
Bonus: Check for contentSize if you want to avoid getting 0 before the content is load:
奖励:如果您想避免在加载内容之前获得 0,请检查 contentSize:
if tableView.contentSize.height > 0 &&
((tableView.contentOffset.y + tableView.safeAreaInsets.top) == 0) { ... }