iOS 11中在导航栏中使用UISearchController和UIRefreshControl会导致布局故障。

13

我正在尝试在表格视图上与新的searchController API一起使用UIRefreshControl
现在,当我设置hidesSearchBarWhenScrolling时,“下拉刷新”动画不再显示,而刷新控件只会在某个特定点弹出。

enter image description here

似乎是UIKit的一个bug(就像每年一样)。有人找到了解决方案吗?

要复制此问题,请将其添加到新的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
}
2个回答

4

我刚刚遇到了同样的问题。这明显是UIKit中的一个bug。提交radar一定是值得的。

不过,我找到了一种非常hacky的方法来缓解这个问题:

- (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,搜索栏将继续保持可见。
希望苹果公司能在未来的iOS版本中修复这个问题,但对于当前的发布版本,这可能是唯一的解决方法。

0
你需要将表格视图的顶部约束设置为Superview,而不是SafeArea。
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接