如何在iOS 8中使用UISearchController,其中UISearchBar位于导航栏中并具有范围按钮? [英] How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?
问题描述
我正在尝试从iOS 8使用新的 UISearchController
,并在我的<$中嵌入 UISearchBar
C $ C> UINavigationBar的。这很容易完成如下:
I'm trying to use the new UISearchController
from iOS 8, and embed its UISearchBar
in my UINavigationBar
. That's easily done as follows:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = searchController.searchBar
但是当我添加范围按钮时:
But when I add the scope buttons:
searchController.searchBar.showsScopeBar = true
searchController.searchBar.scopeButtonTitles = ["Posts, Users, Subreddits"]
它添加了 UISearchBar
后面的按钮,显然看起来很奇怪。
It adds the buttons behind the UISearchBar
and obviously looks very odd.
我应该怎么做?
推荐答案
你正碰到一个设计问题,其中 scopeBar
预计会被隐藏 searchController
未激活。
You're bumping into a "design issue" where the scopeBar
is expected to be hidden when the searchController
is not active.
范围栏按钮显示在搜索栏的后面(下方),因为这是他们的位置搜索栏变为活动状态并将其自身动画到导航栏中。
The scope bar buttons appear behind (underneath) the search bar since that's their location when the search bar becomes active and animates itself up into the navigation bar.
当搜索未激活时,可见范围栏会占用屏幕上的空间,分散内容,并混淆用户(因为范围按钮没有结果可以过滤)。
因为你的 searchBar
已经位于titleView中,(导航和搜索)栏动画显示范围栏不会发生。
Since your searchBar
is already located in the titleView, the (navigation and search) bar animation that reveals the scope bar doesn't occur.
- 最简单选项是在
导航栏下方找到搜索栏,然后让searchBar
动画显示标题
区域激活时。导航栏将为其高度设置动画,
腾出空间以包含隐藏的范围栏。这将是所有
由搜索控制器处理。 -
第二个选项,几乎同样简单,就是使用搜索栏按钮
图标,通过导航栏将searchBar
和scopeBar
设置为
视图的动画。
- 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
andscopeBar
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];
}
如果你想要 searchBar
要保留在titleView中,动画
来做你想要的内容。你必须滚动你自己的
代码来处理navigationBar高度变化并显示你自己的
范围栏(或挂钩到内部,并将内置的
scopeBar
设置为动画并进入视图)。
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).
如果你很幸运,其他人写了 willPresentSearchController:
代码来处理你想要的过渡。
If you're fortunate, someone else has written willPresentSearchController:
code to handle the transition you want.
如果你想总是看到 searchBar
和 scopeBar
,你可能不得不放弃使用内置的 scopeBar
,并将其替换为用户将始终看到的 UISegmentedControl
,即使搜索控制器也是如此不活跃。
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.
更新:
这个答案建议子句ssing UISearchController
更改其searchBar的高度。
This answer suggested subclassing UISearchController
to change its searchBar's height.
这篇关于如何在iOS 8中使用UISearchController,其中UISearchBar位于导航栏中并具有范围按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!