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

UISearchBar gets cut off when using a UINavigationController inside a UITabBarController

我正在尝试在我的 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
}

这是搜索栏的图片:

一旦我点击它:

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

self.extendedLayoutIncludesOpaqueBars = true 

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

尽管我的搜索栏在 iOS 9 中使用搜索控制器的搜索栏上的 sizeToFit 以及 definesPresentationContext=true 在托管搜索结果视图控制器的视图控制器上,iOS 10.

发生了一些变化

对我有用的新修复是在搜索结果视图控制器上禁用 Adjust Scroll View Insets。我只是在 Interface Builder 中做了这个。我必须启用“延伸边缘”。奇怪的是,这使得 Interface Builder 显示 table 单元格在导航栏下被截断,但在运行时并没有被截断。