当在UITabBarController中使用UINavigationController时,UISearchBar会被截断。

5
我正在尝试在UITabBarController的一个选项卡中实现搜索栏,该选项卡是UINavigationController中的UITableViewController...我正在按照苹果的教程进行操作-我尝试了很多不同的选项,包括在此处提到的答案

使用UISearchController和UINavigationController一起使用时,搜索栏被截断

我尝试使用以下属性设置

self.definesPresentationContext = true

或者

self.tabBarController?.definesPresentationContext = true

这是我的代码(来自包含UISearchBar的UITableViewController):
/// Search controller to help us with filtering.
    var searchController: UISearchController!

/// Secondary search results table view.
    var resultsTableController: SearchResultsTableController!

override func viewDidLoad() {
    super.viewDidLoad()
    resultsTableController = SearchResultsTableController()
    resultsTableController.tableView.delegate = self

    searchController = UISearchController(searchResultsController: resultsTableController)
    searchController.searchResultsUpdater = self
    searchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchController.searchBar

    searchController.delegate = self
    searchController.dimsBackgroundDuringPresentation = true
    searchController.searchBar.delegate = self  // so we can monitor text changes

    self.definesPresentationContext = true
}

这是搜索栏的图片:

enter image description here

一旦我轻触它: 在此输入图像描述

2个回答

4

好的,问题终于解决了。这一行让它正常工作了。

self.extendedLayoutIncludesOpaqueBars = true 

我的 TabBar 不是半透明的,所以我认为这不会有任何影响,但是我在我的 UITableviewController 上设置了这个属性(正在显示 UISearchController 的控制器),现在搜索正确地显示在导航栏中。我还将顶部和底部栏的延伸边缘都设为 true(使用界面构建器)。


0

虽然在iOS 9中使用sizeToFit对searchController的搜索栏进行设置,以及在托管搜索结果视图控制器的视图控制器上设置definesPresentationContext=true可以正常工作,但在iOS 10中发生了一些变化。

对我有用的新修复方法是在搜索结果视图控制器上禁用"Adjust Scroll View Insets"。我只是在Interface Builder中进行了这个操作。我不得不保留Extend Edges。很奇怪的是,这使得Interface Builder显示表格单元被导航栏切断,但在运行时它并没有被切断。


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