scopeBar在UISearchBar第一次处于活动状态时不显示

时间:2014-10-17 02:28:16

标签: ios uisearchbar 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;

但是当我在搜索栏中搜索加载视图时。范围栏不会显示。第二个我在搜索栏中搜索,范围栏显示。发生了什么事?

first time search, scope bar don't show second time search, scope bar shows

3 个答案:

答案 0 :(得分:1)

试试这个:

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

根据您的评论,将上述代码放在searchDisplayControllerWillBeginSearch:delegate方法中。这应该有用。

答案 1 :(得分: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()
}

答案 2 :(得分:-1)

您可以删除searchBar.showsScopeBar = YES;