ScopeBar does not appear in UISearchBar when it is active for the first time

I have programmatically created a UISearchController and set the titles of the area buttons in viewDidLoad

UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
[searchResultsController.tableView registerNib:musicNib forCellReuseIdentifier:@"MusicCell"];

self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
UISearchBar *searchBar = self.searchController.searchBar;
searchBar.scopeButtonTitles = @[@"Album", @"Artist", @"Song"];
searchBar.showsScopeBar = YES;
searchBar.selectedScopeButtonIndex = 0;
searchBar.delegate = self;

self.tableView.tableHeaderView = self.searchController.searchBar;

      

But when I go through the search bar while loading the view. the visibility bar is not displayed. The second thing I'm looking for in the search bar is showing the visibility bar. What's happening?

first time search, scope bar don't showsecond time search, scope bar shows

+3


source to share


2 answers


Try the following:

searchBarDisplayController.searchBar.showsScopeBar = NO;
searchBarDisplayController.searchBar.scopeButtonTitles = nil;

      



Based on your comment, put the above code in the searchDisplayControllerWillBeginSearch: delegate method. This should work.

+1


source


You can delete searchBar.showsScopeBar = YES;



-1


source







All Articles