2017-03-09 51 views
1

我有一個導航欄,當屏幕第一次顯示時,其上沒有搜索欄顯示。我在導航欄中觸發並顯示搜索欄,但我想知道如何隱藏搜索欄並再次顯示導航欄標題。如何隱藏UINavigationBar中的UISearchController

我創建的搜索欄如下:

_searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 

self.searchController.searchResultsUpdater = self; 
self.searchController.searchBar.placeholder = nil; 
[self.searchController.searchBar sizeToFit]; 
//self.tableView.tableHeaderView = self.searchController.searchBar; 
self.sharedNavigationItem.titleView = _searchController.searchBar; 

self.searchController.delegate = self; 
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES 
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others 
self.definesPresentationContext = YES; 
_searchController.hidesNavigationBarDuringPresentation = NO; 

我只想在這裏隱藏搜索欄和帶有標題顯示正常導航欄:

enter image description here

+0

您是否嘗試更改self.sharedNavigationItem.titleView? –

回答

1

如果你想隱藏整個導航欄,如果你想要使用你自己的導航離子棒,您可以先隱藏導航控制器的導航欄。

[self.navigationController setNavigationBarHidden:YES]; 
//OR 
[self.navigationController setNavigationBarHidden:YES animated:NO]; 

,或者如果你已經使用自定義標題視圖導航欄,你可以做

self.navigationItem.titleView.hidden = YES; 

,或者如果你想隱藏回欄按鈕的項目,你可以做

self.navigationItem.hidesBackButton = TRUE; 

或者如果你想只隱藏標題,你可以做

self.navigationItem.title = @""; 

And

self.navigationController.navigationItem.titleView = nil ; 
+0

無,我只想隱藏搜索欄並顯示正常的導航欄。 – birdcage

+0

在您的代碼中,您在'titleView'中添加了搜索欄,然後嘗試使用'self.navigationItem.titleView.hidden = YES;'隱藏,然後'self.navigationController.navigationItem.titleView = nil;' –

+0

它沒有幫助。 – birdcage