Joshua
Joshua

Reputation: 15510

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?

Upvotes: 5

Views: 4382

Answers (3)

JOM
JOM

Reputation: 8207

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

Upvotes: 1

Aswathi
Aswathi

Reputation: 91

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

Upvotes: 2

Felixs
Felixs

Reputation: 832

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

Upvotes: 7

Related Questions