2016-08-20 34 views
3

我已經研究了這個問題了很多,但我沒有找到解決方案,將爲我工作。UISearchController不包含狀態欄與不透明UINavigationBar

基本上,我有一個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並顯示鍵盤。然而,在控制器之間的轉換期間發生了一件奇怪的事情

Overlap

它看起來好像UISearchController並不包括狀態欄,讓你看到它呈現以下UIViewController我想找到一種方法來防止這種情況,即強制搜索控制器在狀態欄下一直延伸。我已經做了

事情:

  • 我在viewDidLoad:設置self.definesPresentationContext = true
  • 我發現這是一個已知的bug,即rdar:// 20942583。
  • 我試圖通過設置來規避錯誤:

    self.edgesForExtendedLayout = .All 
    self.extendedLayoutIncludesOpaqueBars = true 
    

    它沒有工作。

我用盡了想法。請幫忙。

謝謝一堆, 皮特。

回答

0

面臨着同樣的問題,並從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 
}