2014-01-29 118 views
3

這裏有很多示例代碼,我認爲我正在關注它的行;我的代碼:displaysSearchBarInNavigationBar不顯示搜索欄

(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // init search bar 
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    searchBar.delegate = self; 
    searchBar.showsCancelButton=YES; 

    // set up searchDisplayController 
    UISearchDisplayController *searchController = [[UISearchDisplayController alloc] 
        initWithSearchBar:searchBar contentsController:self]; 
    searchController.delegate = self; 
    searchController.searchResultsDataSource = self; 
    searchController.searchResultsDelegate = self; 

    // display search bar in nav bar 
    self.searchDisplayController.displaysSearchBarInNavigationBar = YES; 
} 

與該代碼,我不應該看到在導航欄中顯示的搜索欄?我在導航欄中看到的全部是取消按鈕。我在頭文件中聲明瞭協議<UISearchDisplayDelegate, UISearchBarDelegate>。我錯過了什麼,或者會出現什麼問題?謝謝

注意: 使用self.navigationItem.titleView = searchBar,搜索欄按預期顯示。我不知道什麼是新displaysSearchBarInNavigationBar方法的優點..

- 編輯塔希洛通過 -

它看起來像搜索欄添加到導航欄,但隨後消失。

The search bar is only displayed for a few millisecond

回答

1

的displaysSearchBarInNavigationBar特性使得它如此的搜索欄滑入導航欄,當用戶點擊它,就像在蘋果Mail應用程序會發生什麼。您仍然需要將搜索欄添加到某處的視圖。

最好的方法是將UITableViewController的tableHeaderView設置爲搜索欄。

self.tableView.tableHeaderView = self.searchBar; 
+0

我不知道你爲什麼被低估。這對我有效。非常感謝! –

1

這條線是你的問題。

self.searchDisplayController.displaysSearchBarInNavigationBar = YES; 

self.searchDisplayController是不一樣的searchDisplayController,您創建。

因此,只需將self.searchDisplayController替換爲searchDisplayController即可。此外,請確保您沒有@synchronize searchDisplayController;