在iOS 8中,如何使用UISearchController,其中UISearchBar位于我的导航栏中并具有范围按钮?

29

我正在尝试使用iOS 8中的新UISearchController,并将其UISearchBar嵌入到我的UINavigationBar中。操作如下:

searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = searchController.searchBar

但是当我添加了作用域按钮:

searchController.searchBar.showsScopeBar = true
searchController.searchBar.scopeButtonTitles = ["Posts, Users, Subreddits"]

它将按钮添加到 UISearchBar 后面,显然看起来非常奇怪。

我应该如何做这件事?


你能提供一个快照吗? - Yaman
我遇到了同样的问题。似乎UISearchController并不完全理解将UISearchBar放在导航栏内的概念。即使是苹果的示例也似乎将searchBar放在导航栏下方。 - dineth
你使用的是哪个版本?这个问题在 beta 3 和 4 中存在。 - simbesi.com
如果需要,可以跟随此源链接进行帮助:http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view - simbesi.com
1个回答

23
您遇到了一个“设计问题”,其中期望在searchController不活动时隐藏scopeBar
范围栏按钮出现在搜索栏后面(下方),因为当搜索栏变为活动状态并向上动画到导航栏时,它们的位置就在那里。
当搜索未激活时,可见的范围栏会占据屏幕空间,分散用户注意力,并且会让用户感到困惑(因为范围按钮没有结果可过滤)。
由于您的searchBar已经位于titleView中,因此不会发生(导航和搜索)栏动画来显示范围栏。
  • The easiest option is to locate the search bar below the navigation bar, and let the searchBar animate up into the title area when activated. The navigation bar will animate its height, making room to include the scope bar that was hidden. This will all be handled by the search controller.
  • The second option, almost as easy, is to use a Search bar button icon, which will animate the searchBar and scopeBar down into view over the navigation bar.

    - (IBAction)searchButtonClicked:(UIBarButtonItem *)__unused sender {
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        self.searchController.searchResultsUpdater = self;
        self.searchController.hidesNavigationBarDuringPresentation = NO;
        self.searchController.dimsBackgroundDuringPresentation = NO;
        self.definesPresentationContext = YES;
        self.searchController.searchBar.scopeButtonTitles = @[@"Posts", @"Users", @"Subreddits"];
        [self presentViewController:self.searchController animated:YES completion:nil];
    }
  • If you want the searchBar to remain in the titleView, an animation to do what you want is not built in. You'll have to roll your own code to handle the navigationBar height change and display your own scope bar (or hook into the internals, and animate the built-in scopeBar down and into view).

    If you're fortunate, someone else has written willPresentSearchController: code to handle the transition you want.

  • If you want to always see a searchBar and scopeBar, you'll probably have to ditch using the built-in scopeBar, and replace it with a UISegmentedControl which the user will always see, even when the search controller is not active.

更新:

这个答案建议通过子类化UISearchController来改变其搜索栏的高度。


1
第二个选项非常棒(尤其是presentViewController部分用于searchController) - Kedar Paranjape

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接