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?
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)];
(void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)
controller{
_searchBar.showsScopeBar = YES;
[_searchBar sizeToFit];
}
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With