【问题标题】:UISearchController does not cover status bar with opaque UINavigationBarUISearchController 不使用不透明的 UINavigationBar 覆盖状态栏
【发布时间】:2016-08-20 16:48:13
【问题描述】:

我对这个问题进行了很多研究,但没有找到适合我的解决方案。

基本上,我有一个UIViewController,它像这样呈现UISearchController

let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false

searchController.searchBar.delegate = self
view.addSubview(searchController.searchBar)

然后预计用户点击UISearchBar 以呈现searchController 并显示键盘。然而,在控制器之间的转换过程中发生了一件奇怪的事情。

似乎UISearchController 没有覆盖状态栏,让您看到下面显示的UIViewController我想找到一种方法来防止这种情况发生,即强制搜索控制器一直延伸到状态栏下方。

我已经做过的事情:

  • 我在viewDidLoad:中设置了self.definesPresentationContext = true
  • 我发现这是一个已知的bug,即rdar://20942583。
  • 我试图通过设置来规避这个错误:

    self.edgesForExtendedLayout = .All
    self.extendedLayoutIncludesOpaqueBars = true
    

    没用。

我的想法已经不多了。请帮忙。

非常感谢, 皮特。

【问题讨论】:

    标签: ios swift uisearchbar uisearchcontroller


    【解决方案1】:

    面对同样的问题并尝试了 herehere 的所有方法,但这些都不适合我:(

    在我找到更好的解决方案之前有效的最佳解决方法(我知道很难看):

    override func viewDidLoad() {
        super.viewDidLoad()
        searchController.delegate = self
    }
    
    func willPresentSearchController(searchController: UISearchController) {
    
        let statusHeight = UIApplication.sharedApplication().statusBarFrame.size.height
    
        if bgBar == nil {
            bgBar = UIView(frame: CGRectMake(0, 0, view.frame.width, (navigationController?.navigationBar.frame.height)! + statusHeight))
            bgBar.backgroundColor = UIColor.redColor()
            view.addSubview(bgBar)
        } else {
            bgBar.hidden = false
        }
    
        tableView.contentInset.top = statusHeight
    }
    
    func willDismissSearchController(searchController: UISearchController) {
    
        bgBar.hidden = true
        tableView.contentInset.top = 0
    }
    

    【讨论】:

      【解决方案2】:

      在 Swift 4 上:

      viewWillAppear(_ animated: Bool) {
         let statusHeight = UIApplication.shared.statusBarFrame.size.height
         let sbview = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: statusHeight))
         sbview.backgroundColor = .white
         view.addSubview(sbview)
      {
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        • 2014-12-06
        • 1970-01-01
        相关资源
        最近更新 更多