【问题标题】:Fixed UISearchBar using UISearchController - Not using header view of UITableView使用 UISearchController 修复 UISearchBar - 不使用 UITableView 的标题视图
【发布时间】:2023-03-14 23:11:01
【问题描述】:

是否可以将 UISearchController 的 UISearchBar 放在 UITableView 的 header view 以外的地方?

apple's sample code for UISearchController 中,使用了以下内容。

[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar; 

是否可以将 searchBar 放置在其他位置? 假设我们想要实现一个固定的 UISearchBar,就像在联系人应用程序中使用的那样。 我已经尝试过了,但 searchBar 根本没有出现。

【问题讨论】:

    标签: ios uisearchbar uisearchcontroller


    【解决方案1】:

    您可以将 UISearchController 的 UISearchBar 放在导航栏中,使其保持固定

    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    
    // Include the search bar within the navigation bar.
    self.navigationItem.titleView = self.searchController.searchBar;
    
    self.definesPresentationContext = YES;
    

    【讨论】:

    • 在阅读您的答案之前,我正要再次撕掉SearchController 以获得手动管理的UISearchBar,感谢您为我节省了大量时间!
    【解决方案2】:

    创建一个 uiview 并在此视图中添加 searchController.searchbar。 之后在你的 viewController.view 中添加这个视图。在表格视图上方。

    那么它就不会滚动了。

       var viewTemp = UIView(frame: CGRectMake(0.0, 64.0,320.0, 44))
       viewTemp.addSubview(self.searchController.searchBar)
       self.view.addSubview(viewTemp);
    

    【讨论】:

    • 当您不想让 UISearchBar 成为 UITableView 的标题视图时,这似乎是解决方案。谢谢!
    • @AndrewR 在这种情况下。只需执行 tableView.tableHeaderView = searchController.searchBar
    • 这行得通,只是它搞乱了部分索引滚动:(
    • 临时视图的意义何在?为什么不直接添加 UISearchBar?
    【解决方案3】:

    而不是直接将 UISearchBar 保留在 UITableView 之上。 将 UISearchBar 放在视图中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多