UISearchBar Does Not Display Visibility Bar

I am using the following code to display UISearchBarusing the area bar.

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];

However, the visibility bar is never displayed. Why is this not displayed and how can I fix it?

+5
source share
3 answers

c initWithFrame:CGRectMake(0, 0, 320, 45), height is just the height of the search bar, not including the visibility bar. Try replacing something like:

UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 90)];
+7
source
(void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)
 controller{  
    _searchBar.showsScopeBar = YES;
    [_searchBar sizeToFit];
}
+1
source

If you do not have a UISearchDisplayController available, you can try using the UISearchBarDelegate callbacks :

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

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

All Articles