mforrest
mforrest

Reputation: 104

iOS Search Bar in Navigation Bar

I'm currently developing an application that has a MapView as the main view. 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.

Search prompt button

After search button is pressed (excuse the '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

Upvotes: 0

Views: 5555

Answers (2)

Victor Sigler
Victor Sigler

Reputation: 23449

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

See my answer of this here Displaying search bar in navigation bar in 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).

I strongly recommend the way of Twitter does, is more cleanest.

I hope this help you.

Upvotes: 4

ahmedHegazi
ahmedHegazi

Reputation: 300

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

create a UIView and in the navigationItem.tittleView assign your view to it like this 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

Upvotes: 1

Related Questions