UISearchBar未显示范围栏

时间:2010-08-28 12:02:46

标签: iphone objective-c cocoa-touch uisearchbar

我使用以下代码显示带有范围栏的UISearchBar

UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
searchBar.barStyle = UIBarStyleDefault;
searchBar.showsCancelButton = NO;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;

searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"One", @"Two", nil];
searchBar.showsScopeBar = YES;

self.tableView.tableHeaderView = searchBar;

[searchBar release];

但是,范围栏永远不会显示。为什么没有显示,我该如何解决?

3 个答案:

答案 0 :(得分:7)

<{1>}中的

,高度只是搜索栏的高度,不包括范围栏。尝试用以下内容替换:

initWithFrame:CGRectMake(0, 0, 320, 45)

答案 1 :(得分:1)

(void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)
 controller{  
    _searchBar.showsScopeBar = YES;
    [_searchBar sizeToFit];
}

答案 2 :(得分:0)

如果您没有UISearchDisplayController,可以尝试使用 UISearchBarDelegate 回调:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
  self.searchBar.showsScopeBar = YES;
  [self.searchBar sizeToFit];
  return YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
  self.searchBar.showsScopeBar = NO;
  [self.searchBar sizeToFit];
}