我正在尝试在表格视图上使用
UIRefreshControl
以及 searchController
上的新 navigationItem
API。hidesSearchBarWhenScrolling
时,“下拉刷新”动画不再显示,刷新控件只是在某个点弹出。
这似乎是 UIKit 中的一个错误(...与每年的程序相同)。 有人找到这个问题的解决方案吗?
要重现该问题,请将其添加到新的 iOS 11“主/细节”示例项目中:
- (void)viewDidLoad {
// [setup code here]
self.refreshControl = [UIRefreshControl new];
self.navigationItem.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.navigationItem.hidesSearchBarWhenScrolling = NO; // <-- setting this causes jumpy UI
}
我刚刚遇到了同样的问题。它看起来确实像是 UIKit 中的一个错误。这绝对是值得提交雷达的事情。
我发现了一个非常老套的方法来缓解它:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//Fixes a bug in UIKit where the refresh control is broken when `hidesSearchBarWhenScrolling` is NO.
if (@available(iOS 11.0, *)) {
self.navigationItem.hidesSearchBarWhenScrolling = scrollView.contentOffset.y < -scrollView.adjustedContentInset.top;
}
}
基本上,这里发生的事情是,每当滚动视图滚动到顶部(刷新控件将变得可见)时,这段代码就会将
hidesSearchBarWhenScrolling
返回到 YES
。一旦用户再次向下滚动,它将被设置回 NO
并且搜索栏将继续保持可见。
希望 Apple 能够在未来的 iOS 版本中修复此问题,但对于当前的发布版本,这可能是必须要做的。
您需要将 Table View 的顶部约束设置为 Superview,而不是 SafeArea。
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true