简体   繁体   English

导航栏中的iOS搜索栏

[英]iOS Search Bar in Navigation Bar

I'm currently developing an application that has a MapView as the main view. 我目前正在开发一个具有MapView作为主视图的应用程序。 I want to have a search button in the navigation bar that brings up a search bar within the same navigation bar. 我想在导航栏中找到一个搜索按钮,以在同一导航栏中显示一个搜索栏。

If you've used the Twitter app you'll know what I'm talking about. 如果您使用过Twitter应用程序,那么您会知道我在说什么。

搜索提示按钮

按下搜索按钮后(请使用“ McWhopper”)

I've seen a ton of guides and tutorials that show how to implement something similar to this in a table view however this isn't what I want to do. 我看过无数的指南和教程,它们显示了如何在表视图中实现与此类似的操作,但这不是我想要的。

Can anyone tell me what the best way to do this would be? 谁能告诉我最好的方法是什么?

As somebody answered very quickly pointed out - my question looks very similar to this question. 正如有人回答非常迅速指出-我的问题看起来非常相似, 这一问题。 However nothing on that question seemed to work for me and it doesn't address the issue of getting the search bar to appear on a button press. 但是,这个问题似乎对我没有任何帮助,它也无法解决在按钮按下时出现搜索栏的问题。

As it is right now my search doesn't need a SearchViewController as all it does is use an MKLocalSearch to position the MapView 现在,我的搜索不需要SearchViewController因为它只需要使用MKLocalSearchMKLocalSearch MapView

First of all , you need to know that what Twitter does is present a new UIViewController when the search button is tapped , but without animation. 首先,您需要知道,当点击搜索按钮时,Twitter会显示一个新的UIViewController ,但没有动画。 In this new UIViewController you can put your UISearchBar in code like in the answer you put in your question or using Interface Builder, it's up to you. 在这个新的UIViewController您可以将UISearchBar放入代码中,就像在问题中输入答案一样,也可以使用Interface Builder,由您自己决定。

See my answer of this here Displaying search bar in navigation bar in iOS 8 在此处查看我的答案在iOS 8的导航栏中显示搜索栏

If want you want to do is present the UISearchBar in the same UIViewController where the search icon appears, the you need to handle an @IBAction for the UIBarButtonItem and the change it according your needs (with animation if you want). 如果要执行的操作是在显示搜索图标的同一UIViewController中显示UISearchBar ,则需要处理@IBActionUIBarButtonItem并根据需要进行更改(如果需要,可以使用动画)。

I strongly recommend the way of Twitter does, is more cleanest. 我强烈建议Twitter做的方式,最简洁。

I hope this help you. 希望对您有所帮助。

I'm going to give you an idea i tried myself and it is working but i can't provide you a code right now 我要给您一个我尝试过的想法,它可以正常工作,但现在无法为您提供代码

I'm sure you can find the code easily of what i am gonna tell you. 我相信您可以轻松找到我要告诉您的代码。

first you have to know that in the navigation controller the title is in UIView that you can access so all you need to do is 首先,您必须知道导航控制器中的标题位于您可以访问的UIView中,因此您所要做的就是

create a UIView and in the navigationItem.tittleView assign your view to it like this navigationItem.tittleView = the created view 创建一个UIView,并在navigationItem.tittleView中将您的视图分配给它,就像这个navigationItem.tittleView = the created view

this create a search bar object and add it the created view as subview 这将创建一个搜索栏对象并将其添加为子视图

view.addSubView(the search bar view object)

hope this is what you are looking for .... good luck 希望这是您正在寻找的..祝您好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM