第一次激活时,scopeBar 不会显示在 UISearchBar 下

yon*_* ho 5 uisearchbar ios uisearchcontroller

我以编程方式创建了一个 UISearchController,并在 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;
Run Code Online (Sandbox Code Playgroud)

但是当我在加载视图时在搜索栏中搜索时。范围栏不显示。我在搜索栏中搜索的第二次,范围栏显示。这是怎么回事?

第一次搜索,范围栏不显示 第二次搜索,范围栏显示

小智 -2

您可以删除searchBar.showsScopeBar = YES;