6

我正在創建UISearchViewController,我希望將UISearchBar置於導航控制器的導航項中。以下是我沒有結果做到了:UISearchBar的範圍欄沒有在iOS 7中顯示?

self.searchBar = [UISearchBar new]; 
self.searchBar.showsScopeBar = YES; 
self.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"]; 
self.searchBar.delegate = self; 

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self]; 
self.searchController.delegate = self; 
self.searchController.searchResultsDataSource = self; 
self.searchController.searchResultsDelegate = self; 
self.searchController.displaysSearchBarInNavigationBar = YES; 

我也試過:

self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleMinimal; 
self.searchDisplayController.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"]; 
self.searchDisplayController.searchBar.showsScopeBar = YES; 

我缺少了任何東西,或在這裏做錯了什麼?

編輯:Thisthis正是我的目標。

+0

你找到這是一個解決方案嗎?我遇到了同樣的問題 –

+0

@KongHantrakool我還沒有找到解決方案。同時,我在導航欄中使用UISegmentedControl來完成範圍欄的工作。 – Ryan

+0

感謝您的回答 –

回答

1

嘗試用下面的代碼

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero]; 
searchBar.barStyle = UIBarStyleBlack; 
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Users", @"Hashtags", nil]; 
searchBar.showsScopeBar = YES; 
self.searchBar.delegate = self; 
+1

不幸的是,範圍欄仍然沒有顯示。只是導航欄中的搜索欄。 – Ryan

1

範圍欄時,搜索欄活動纔會出現。搜索顯示控制器隱藏搜索欄的範圍欄。

如果您希望範圍欄始終可用,您將不得不做一些小工作。也許其中一個會有所幫助。

How to have UISearchBar scope bar always visible?

IOS7 : uisearchdisplaycontroller always show scope bar

編輯:

您是否嘗試過在這個方向上的東西嗎?假設您已將搜索欄添加到導航欄的titleView

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 

    CGRect rect = self.navigationItem.titleView.superview.frame; 
    rect.size.height = 88.0f; 
    self.navigationItem.titleView.superview.frame = rect; 
} 
+0

嗨,我沒有問題,當酒吧在self.view或tableView.headerView時顯示範圍欄。只要搜索欄位於導航欄中,範圍欄就不會顯示,既可以將其添加到視圖中,然後將該視圖設置爲導航欄的標題視圖,也可以通過「self.searchController.displaysSearchBarInNavigationBar = YES;」進行設置。 – Ryan

+0

啊,這聽起來像是從導航欄中刪除範圍欄。你有沒有試過擴展導航欄的高度?可能在視圖控制器-viewDidLayoutSubviews中執行此項工作。 – cnotethegr8

+0

您是否找到了解決方案?我遇到同樣的問題 –

0

嘗試調整搜索欄架: -

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake:(0,0,320,88)]; 
+0

searchDisplayController會在搜索取消後將searchBar的高度重置爲44。 – cnotethegr8

0

我設置爲 「使用自動佈局」 從XIP檢查 - >界面構建

那是我完美的工作

相關問題