Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar is not displaying Scope Bar

I use the following code to display a UISearchBar with a Scope 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 scope bar is never displayed. Why is it not being displayed and how can I fix this?

like image 480
Joshua Avatar asked Aug 28 '10 12:08

Joshua


3 Answers

in the initWithFrame:CGRectMake(0, 0, 320, 45), the height is only the height of a search bar, not including a scope bar. Try replacing with something like:

UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 90)];
like image 145
Felixs Avatar answered Sep 30 '22 19:09

Felixs


(void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)
 controller{  
    _searchBar.showsScopeBar = YES;
    [_searchBar sizeToFit];
}
like image 30
Aswathi Avatar answered Sep 30 '22 18:09

Aswathi


If you don't have UISearchDisplayController available, you could try using 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];
}
like image 20
JOM Avatar answered Sep 30 '22 19:09

JOM