3

我有一個在頂部搜索欄一個UITableView。我使用UISearchDisplayController來實現相同的功能。還有一個帶兩個按鈕的範圍欄。在默認情況下,當我啓動應用程序時,範圍欄將顯示。在搜索後單擊取消按鈕時,範圍欄消失。所以,即使在我按下「取消」按鈕後,還有什麼方法可以保持範圍欄。我用下面的代碼,但它不工作。即使按下取消按鈕後,如何保持範圍欄?

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar 
{ 
    [searchBar setShowsScopeBar:YES]; 
    return YES; 
} 

謝謝:)

回答

5

我有今天的問題,我想我已經找到了解決辦法。

你需要做兩件事情:

  1. 呼叫 'sizeToFit' 關於設置showsScopeBar後的搜索欄。這將確保searchBars框架設置正確,以包含範圍欄。
  2. 不幸的是,表視圖似乎並不當搜索欄調整大小,並造成範圍欄重疊所述第一小區的喜歡。爲了解決這個問題,你可以重新設置tableHeaderView作爲searchBar(再次),這似乎解決了重疊問題。

最終代碼:

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar 
{ 
    self.searchBar.showsScopeBar = YES; 
    [self.searchBar sizeToFit]; 
    self.tableView.tableHeaderView = self.searchBar; 
    return YES; 
} 
+0

w ^雖然我可以在iOS6中使用此修復程序,但它在iOS7中無法使用。有沒有辦法讓它工作,同時還要維護搜索顯示控制器? – msec

+1

此解決方案不適用於iOS 8.請建議。 – user1544494

4

我也一直在爭奪與這類UISearchBarController問題,最近幾天我自己,我不得不說做什麼異常與一個UISearchBar是最好的方式根本不使用UISearchDisplayController

只需使用UISearchBarUISearchBarDelegate方法並自行打印,然後您就可以將所有內容設置爲完全按照您的要求操作。

這裏是我在最近的一個項目做。 - 範圍欄始終保持可見 - 我立即過濾作爲文本輸入 - 我立即過濾,如果範圍是改變 - 我隱藏取消按鈕時,不需要的話 - 我隱藏鍵盤的不需要的時候

// Filters the table when requested 
- (void)filterContentForSearchBar:(UISearchBar *)searchBar 
{ 
    NSString *scope = [[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]]; 
    NSString *search = [searchBar text]; 

    NSMutableArray *predicates = [[NSMutableArray alloc] init]; 

    if ([scope isEqualToString:@"Selected"]) 
    { 
     [predicates addObject:[NSPredicate predicateWithFormat:@"selected == 1"]]; 
    } 

    if (search && search.length) { 
     [predicates addObject:[NSPredicate predicateWithFormat:@"name contains[cd] %@", search]]; 
    } 

    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates]; 

    self.filteredObjectList = [self.objectList filteredArrayUsingPredicate:predicate]; 

    [self.myTableView reloadData]; 
} 


#pragma mark - UISearchBarDelegate Methods 

// React to any delegate method we are interested in and change whatever needs changing 
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    searchBar.showsCancelButton = true; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    searchBar.showsCancelButton = false; 
    [searchBar resignFirstResponder]; 

    searchBar.text = nil; 
    [self filterContentForSearchBar:searchBar]; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    searchBar.showsCancelButton = false; 
    [searchBar resignFirstResponder]; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    [self filterContentForSearchBar:searchBar]; 
} 

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope 
{ 
    [self filterContentForSearchBar:searchBar]; 
} 

偉大工程:)

+0

我只用的UISearchBar並取得範圍和通過在段頭中添加的代替的tableview頭與縱向高度= 44.0f * 2和景觀的UISearchBar永遠取消按鈕可見= 44.0f一些性質searchBar.showsCancelButton = YES,searchBar.showsScopeBar = YES –

+0

@trapper:太好了! – sabiland

0

這個answer的比較正確的做法是添加邏輯返回結果:

@property (nonatomic) BOOL shouldHideFirstResponder; //assign YES in init 

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { 
    if (self.shouldHideFirstResponder) { 
     self.searchBar.showsScopeBar = YES; 
     [self.searchBar sizeToFit]; 
     self.table.tableHeaderView = self.searchBar; 
    } 

    return self.shouldHideFirstResponder; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    self.shouldHideFirstResponder = NO; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar { 
    self.shouldHideFirstResponder = YES; 
} 

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { 
    if (!self.searchBar.text || self.searchBar.text.length < 1) { 
     self.shouldHideFirstResponder = YES; 
    } 
} 
相關問題