5

我以编程方式创建了一个 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;

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

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

4

3 回答 3

1

尝试这个:

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

根据您的评论,将上述代码放在 searchDisplayControllerWillBeginSearch: 委托方法中。那应该行得通。

于 2016-03-31T11:06:04.183 回答
0
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()
}
于 2020-03-17T04:38:38.937 回答
-2

你可以删除searchBar.showsScopeBar = YES;

于 2015-03-03T09:37:02.560 回答