2015-04-28 73 views
9

我想在我的tableViewController中使用新的UISearchController。UISearchController in navigationBar

但是我有點困惑,當我按下searchBar時,它可以在navigationController中向上移動,它與舊的searchDisplayController的關係如何?

目前它只停留在tableHeader中。

這裏是我的代碼:

self.teamSearchController = ({ 
     let controller = UISearchController(searchResultsController: nil) 

     controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal 
     controller.dimsBackgroundDuringPresentation = false 
     controller.searchBar.sizeToFit() 
     controller.searchBar.showsScopeBar = true 
     self.tableView.tableHeaderView = controller.searchBar 

     return controller 
    })() 

控制器:

enter image description here

當我點擊搜索欄:

enter image description here

+0

刪除'controller.hidesNavigationBarDuringPresentation = FALSE'也應該給你你期待 –

+0

我加2圖像這descripe行爲 –

+0

設定searchController的hidesNavigationBarDuringPresentation爲真,並嘗試切換您的ViewController的definesPresentationContext看到結果的行爲。從不隱藏導航欄DuringPresentation爲false,如果您想要提及的行爲,請不要在navigationItem的titleView上放置搜索欄。 – Solomon

回答

16

您可以將的導航欄,而不是表頭

self.searchController.hidesNavigationBarDuringPresentation = NO; 
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal; 

// Include the search bar within the navigation bar. 
self.navigationItem.titleView = self.searchController.searchBar; 

self.definesPresentationContext = YES; 
+0

我已經添加了2張描述此行爲的圖像 –

+0

您是否在此答案中嘗試瞭解決方案? –

+0

不,但是這將從頭開始添加到頂部,並且不像searchDisplayController那樣具有相同的行爲 –

2

Swift版本:

self.searchController.hidesNavigationBarDuringPresentation = false 
self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal 

// Include the search bar within the navigation bar. 
self.navigationItem.titleView = self.searchController.searchBar 
self.definesPresentationContext = true 
0

它與導航欄做。

func willPresentSearchController(searchController: UISearchController) { 
    self.navigationController?.navigationBar.translucent = true 
} 

func willDismissSearchController(searchController: UISearchController) { 
    self.navigationController?.navigationBar.translucent = false 
} 

如果你這樣做,那麼你將能夠將它添加到表視圖標題並獲得烘焙搜索控制器動畫!