【问题标题】:Section header on top of UISearchBar on iOS 8. Works on iOS 9iOS 8 上 UISearchBar 顶部的部分标题。适用于 iOS 9
【发布时间】:2015-08-10 13:52:17
【问题描述】:

使用 UISearchController 将过滤添加到 UITableView。部分标题与使用 Xcode 7 的 iOS 8 上的搜索栏重叠。这在 iOS 9 中看起来不错。我可以采取什么解决方法使 iOS 8 看起来像 iOS 9 版本?

override func viewDidLoad() {
    super.viewDidLoad()
    self.searchController.searchResultsUpdater = self
    self.searchController.hidesNavigationBarDuringPresentation = false
    self.searchController.dimsBackgroundDuringPresentation = false
    self.tableView.tableHeaderView = searchController.searchBar
    self.definesPresentationContext = true
    self.searchController.searchBar.sizeToFit()

    tableView.sectionIndexBackgroundColor = UIColor.clearColor()
}

【问题讨论】:

  • 我目前遇到了同样的问题 - 你找到解决方法了吗?
  • @Harry 在将其设置为 tableHeaderView 之前,我很幸运地在 searchBar 上调用了 sizeToFit()

标签: ios8 xcode7 ios9 uisearchcontroller


【解决方案1】:
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
[self.tableView.tableHeaderView addSubview:self.searchController.searchBar];

新建一个UIView并将searchBar添加为子视图,然后将tableView的header view设置为新创建的view即可解决问题。

【讨论】:

    【解决方案2】:

    更改为:

        self.searchController.searchResultsUpdater = self
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = false
        self.searchController.definesPresentationContext = true
        self.searchController.searchBar.sizeToFit()
        self.navigationItem.titleView = self.searchController.searchBar;
        tableView.sectionIndexBackgroundColor = UIColor.clearColor()
    

    所以在 searchController 上设置定义PresentationContext,而不是在视图控制器上。

    【讨论】:

    • 如何将 seachBar 放在 titleView 而不是 tableHeaderView 中解决了这里的问题?这并没有真正解决问题......
    猜你喜欢
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多