导航栏中的 UISearchBar

发布于 2024-10-19 06:41:30 字数 65 浏览 8 评论 0原文

如何在导航栏中显示 UISearchBar?

我不知道该怎么做。

非常感谢您的帮助。

How can I show a UISearchBar in the NavigationBar?

I can't figure out how to do this.

Your help is very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼前雾蒙蒙 2024-10-26 06:41:30

将 searchBar 置于 navigationBar 的中心:

self.navigationItem.titleView = self.searchBarTop;

将 searchBar 置于 navigationBar 的左侧/右侧:

UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;

To put searchBar into the center of navigationBar:

self.navigationItem.titleView = self.searchBarTop;

To put searchBar to the left/right side of navigationBar:

UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
深爱成瘾 2024-10-26 06:41:30

从 iOS 7 开始,UISearchDisplayController 默认支持此功能。设置 UISearchDisplayController 的 displaysSearchBarInNavigationBar = YES 即可轻松实现此操作。

根据文档:

从 iOS 7.0 开始,您可以通过配置搜索显示控制器的 displayedSearchBarInNavigationBar 和 navigationItem 属性,将搜索显示控制器与导航栏(UINavigationBar 类的实例)结合使用。

As of iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES to get this working easily.

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.

倦话 2024-10-26 06:41:30

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

我发现使用 iOS7 在 searchDisplayController 上的新属性将 searchBar 添加到 navigationBar 的两种不同方法。

1) 基于 Nib 的方法

如果您使用 .xib,则可以为此值设置用户定义的运行时属性,并且无论出于何种原因,leftBarButtonItem 都会保持不变。我还没有使用 rightBarButtonItem 对其进行测试。

在此处输入图像描述

2) 代码(时机很重要)

如果您想用代码实现,时机似乎很重要。看来您必须首先将searchBar添加到navigationBar,然后设置您的barButtonItem

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

As one commenter noted, using searchDisplayController.displaysSearchBarInNavigationBar = true ends up hiding any existing left/right bar button items.

I've found two different ways of adding a searchBar to a navigationBar using iOS7's new property on searchDisplayController.

1) Nib Based Approach

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.

enter image description here

2) Code (Timing Matters)

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.

- (void)viewDidLoad
{
    ...
    self.searchDisplayController.displaysSearchBarInNavigationBar = true;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
    ...
}
眉目亦如画i 2024-10-26 06:41:30

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

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

醉殇 2024-10-26 06:41:30

导航栏中 UISearchBar 的目标 C 代码片段

- (void)viewDidLoad {
    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.searchController = searchController;
    } else {
        self.navigationItem.titleView = searchController.searchBar;
    }
}

阅读更多内容 这里

Objective C code snippet for UISearchBar in NavigationBar

- (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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文