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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:59:31  来源:igfitidea点击:

UISearchBar in navigationbar

iosuinavigationbaruisearchbar

提问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 = YESto 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 = trueends up hiding any existing left/right bar button items.

正如一位评论者指出的那样,使用searchDisplayController.displaysSearchBarInNavigationBar = true最终会隐藏任何现有的左/右栏按钮项目。

I've found two different ways of adding a searchBarto a navigationBarusing 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 测试过它。

enter image description here

在此处输入图片说明

2) Code (Timing Matters)

2) 代码(时间问题)

If you want to implement in code, timing seems to matter. It seems that you must add the searchBarto the navigationBarfirst, then set your barButtonItem.

如果你想在代码中实现,时间似乎很重要。似乎您必须将searchBar加到第navigationBar一个,然后设置您的barButtonItem.

- (void)viewDidLoad
{
    ...
    self.searchDisplayController.displaysSearchBarInNavigationBar = true;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
    ...
}

回答by Steve Moser

Check out Apple's UICatalogsample code. It shows how to use the new UISearchControllerin three different ways: modally, in nav bar, and below the navigation bar.

查看 Apple 的UICatalog示例代码。它展示了如何以UISearchController三种不同的方式使用新的:模态、导航栏和导航栏下方。

回答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

在这里阅读更多