Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar's scope bar is not showing in iOS 7?

I'm creating a UISearchViewController and I wish to put the UISearchBar in the navigation controller's navigation item. Here's how I did it with no results:

self.searchBar = [UISearchBar new];
self.searchBar.showsScopeBar = YES;
self.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"];
self.searchBar.delegate = self;

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.displaysSearchBarInNavigationBar = YES;

I also tried:

self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchDisplayController.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"];
self.searchDisplayController.searchBar.showsScopeBar = YES;

Am I missing out anything or doing something wrong here?

Edit: This and this is exactly what I'm aiming for.

like image 203
Ryan Avatar asked Feb 26 '14 06:02

Ryan


2 Answers

Try with following code

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Users", @"Hashtags", nil];
searchBar.showsScopeBar = YES;
self.searchBar.delegate = self;
like image 169
iPatel Avatar answered Oct 21 '22 10:10

iPatel


The scope bar will only appear when the search bar is active. The search display controller is hiding the search bars scope bar.

If you want the scope bar always available, you'll have to do a little hack work. Maybe one of these will help.

How to have UISearchBar scope bar always visible?

IOS7 : uisearchdisplaycontroller always show scope bar

EDIT:

Have you tried something in this direction? Assuming you have added the search bar to the navigation bar's titleView.

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGRect rect = self.navigationItem.titleView.superview.frame;
    rect.size.height = 88.0f;
    self.navigationItem.titleView.superview.frame = rect;
}
like image 45
cnotethegr8 Avatar answered Oct 21 '22 09:10

cnotethegr8