【问题标题】:scopeBar don't show under UISearchBar when it is active for the first time第一次处于活动状态时,scopeBar 不显示在 UISearchBar 下
【发布时间】:2020-03-17 12:38:38
【问题描述】:

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

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

【问题讨论】:

    标签: ios uisearchbar uisearchcontroller


    【解决方案1】:

    试试这个:

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

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

    【讨论】:

      【解决方案2】:
      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()
      }
      

      【讨论】:

        【解决方案3】:

        您可以删除searchBar.showsScopeBar = YES;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-06-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-22
          • 2018-11-14
          • 2018-05-29
          相关资源
          最近更新 更多