呈现 UISearchController 与状态栏重叠

Presenting UISearchController overlaps with statusbar

我有一个 UISearchController,它会在用户触摸按钮时以编程方式呈现。我的问题是搜索栏与状态重叠(见屏幕截图)

我有以下代码用于显示 UISearchController

func presentSearchController() {
    let resultsController = ResultsViewController()

    self.searchController = UISearchController(searchResultsController: resultsController)
    self.searchController.searchBar.searchBarStyle = .prominent

    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchResultsUpdater = resultsController

    self.definesPresentationContext = true

    self.present(self.searchController, animated: true, completion: nil)
}

编辑:我的问题不是 UISearchBar overlaps status bar in iOS 的重复问题,因为我没有直接使用搜索栏或其框架

我已经通过删除以下行解决了这个问题:

self.definesPresentationContext = true

在我的场景中不需要这样做的原因是我正在调用 self.present(self.searchController, animated: true, completion: nil)

更新:

虽然上面确实解决了我的问题,但它破坏了在显示 SearchController 时在我的 navigationController 中推送 viewController 的功能(searchController 将保持在顶部,而 viewController 被推到下面它)

在重新审视这个问题后,以下是我所做的修复:

添加以下内容

searchController.hidesNavigationBarDuringPresentation = true self.definesPresentationContext = true

然后在故事板中启用"Under Top Bars"并启用"Under Opaque Bars"