Ios scopeBar don';当UISearchBar第一次处于活动状态时,它不会显示在UISearchBar下

Ios scopeBar don';当UISearchBar第一次处于活动状态时,它不会显示在UISearchBar下,ios,uisearchbar,uisearchcontroller,Ios,Uisearchbar,Uisearchcontroller,我以编程方式创建了一个UISearchController,并在viewDidLoad中设置了范围按钮标题 UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain]; searchResultsController.tableView.dataSource = self; searchResultsController.t

我以编程方式创建了一个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;
但当我在搜索栏中搜索时,视图已加载。范围栏不显示。第二次我在搜索栏中搜索时,范围栏显示。发生什么事了


您可以删除
searchBar.showscopebar=YES

尝试以下操作:

searchBarDisplayController.searchBar.showsScopeBar = NO;
searchBarDisplayController.searchBar.scopeButtonTitles = nil;
根据您的评论,将上述代码放入searchDisplayControllerWillBeginSearch:delegate方法中。这应该行得通

searchController.delegate = self

// in the delegate...

func willPresentSearchController(_ searchController: UISearchController) {
    searchController.searchBar.showsScopeBar = true
    searchController.searchBar.sizeToFit()
}

func willDismissSearchController(_ searchController: UISearchController) {
    searchController.searchBar.showsScopeBar = false
    searchController.searchBar.sizeToFit()
}