在 UITabBarController 中使用 UINavigationController 时,UISearchBar 被切断

Posted

技术标签:

【中文标题】在 UITabBarController 中使用 UINavigationController 时,UISearchBar 被切断【英文标题】:UISearchBar gets cut off when using a UINavigationController inside a UITabBarController 【发布时间】:2015-12-09 04:28:29 【问题描述】:

我正在尝试在我的 UITabBarController 中的一个选项卡中实现搜索栏,该选项卡是 UINavigationController 中的 UITableViewController ...我正在关注 Apple 教程 - 我尝试了很多不同的选项,包括此处提到的答案

Search bar gets cut off when using UISearchController with a UITabBarController inside a 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

这是搜索栏的图片:

一旦我点击它:

【问题讨论】:

【参考方案1】:

好的,终于解决了这个问题。这条线让它工作

self.extendedLayoutIncludesOpaqueBars = true 

我的 TabBar 不是半透明的,所以我认为这不会产生影响,但我在我的 UITableviewcontroller(显示 UISearchController 的控制器)上设置了它,现在搜索显示在导航栏中正确。我还将顶部和底部栏下的扩展边缘设置为 true(使用 Interface Builder)

【讨论】:

【参考方案2】:

虽然我的搜索栏在 ios 9 中使用 searchController 的 searchBar 上的 sizeToFit 以及 definesPresentationContext=true 在托管搜索结果视图控制器的视图控制器上,iOS 10 发生了一些变化。

对我有用的新修复是禁用搜索结果视图控制器上的调整滚动视图插图。我只是在 Interface Builder 中做到了这一点。我不得不启用扩展边缘。奇怪的是,这使得 Interface Builder 在导航栏下显示表格单元格被截断,但在运行时并没有被截断。

【讨论】:

以上是关于在 UITabBarController 中使用 UINavigationController 时,UISearchBar 被切断的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有 UITabBarController 的情况下使用 UITabBar

UITabBarController的基本原理及使用

在 xcode 4.3 中使用 UITableViewController xib 创建 UITabbarController

在 UITabBarController 中,moreNavigationController 始终为零

使用 UITabBarController 的 JASidePanels

如何在 UITabBarController 中使用 Viper 架构