ios 导航栏中的 UISearchBar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5095477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISearchBar in navigationbar
提问by Samui
How can I show a UISearchBar in the NavigationBar?
如何在 NavigationBar 中显示 UISearchBar?
I can't figure out how to do this.
我无法弄清楚如何做到这一点。
Your help is very much appreciated.
非常感激你的帮助。
回答by Borut Tomazin
To put searchBar into the center of navigationBar:
将 searchBar 置于导航栏的中心:
self.navigationItem.titleView = self.searchBarTop;
To put searchBar to the left/right side of navigationBar:
将 searchBar 放在导航栏的左侧/右侧:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
回答by James Kuang
As of iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES
to get this working easily.
从 iOS 7 开始, UISearchDisplayController 默认支持这个。设置 UISearchDisplayControllerdisplaysSearchBarInNavigationBar = YES
以使其轻松工作。
Per the documentation:
根据文档:
Starting in iOS 7.0, you can use a search display controller with a navigation bar (an instance of the UINavigationBar class) by configuring the search display controller's displaysSearchBarInNavigationBar and navigationItem properties.
从 iOS 7.0 开始,您可以通过配置搜索显示控制器的 displaySearchBarInNavigationBar 和 navigationItem 属性来使用带有导航栏(UINavigationBar 类的实例)的搜索显示控制器。
回答by djibouti33
As one commenter noted, using searchDisplayController.displaysSearchBarInNavigationBar = true
ends up hiding any existing left/right bar button items.
正如一位评论者指出的那样,使用searchDisplayController.displaysSearchBarInNavigationBar = true
最终会隐藏任何现有的左/右栏按钮项目。
I've found two different ways of adding a searchBar
to a navigationBar
using iOS7's new property on searchDisplayController
.
我发现有两种不同的方法searchBar
可以navigationBar
使用 iOS7 的新属性将 a 添加到 a上searchDisplayController
。
1) Nib Based Approach
1) 基于笔尖的方法
If you're using a .xib, you can set a User Defined Runtime Attribute for this value and for whatever reason, the leftBarButtonItem stays in tact. I have not tested it with a rightBarButtonItem.
如果您使用的是 .xib,您可以为此值设置用户定义的运行时属性,无论出于何种原因,leftBarButtonItem 都会保持原状。我还没有用 rightBarButtonItem 测试过它。
2) Code (Timing Matters)
2) 代码(时间问题)
If you want to implement in code, timing seems to matter. It seems that you must add the searchBar
to the navigationBar
first, then set your barButtonItem
.
如果你想在代码中实现,时间似乎很重要。似乎您必须将searchBar
加到第navigationBar
一个,然后设置您的barButtonItem
.
- (void)viewDidLoad
{
...
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
...
}
回答by Steve Moser
回答by yoAlex5
Objective C code snippet
目标 C 代码片段
- (void)viewDidLoad {
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
if (@available(iOS 11.0, *)) {
self.navigationItem.searchController = searchController;
} else {
self.navigationItem.titleView = searchController.searchBar;
}
}
Read more here
在这里阅读更多