我正在尝试使用来自 iOS 8 的新 UISearchController
,并将其 UISearchBar
嵌入到我的 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.
我应该怎么做?
你遇到了一个设计问题",当 searchController
scopeBar 应该被隐藏代码> 未激活.
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
向下移动到查看导航栏.
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];
}
如果你想让 searchBar
留在 titleView 中,一个动画做你想做的事不是内置的.你必须自己动手处理导航栏高度变化并显示您自己的代码范围栏(或挂钩到内部,并动画内置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.
更新:
此答案建议子类化 UISearchController
以更改其 searchBar 的高度.
This answer suggested subclassing UISearchController
to change its searchBar's height.
这篇关于如何在 iOS 8 中使用 UISearchController,其中 UISearchBar 在我的导航栏中并且有范围按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!