Simon
Simon

Reputation: 25983

How can I put a search bar in (not under) a unnavigationbar?

I'd like to put a search bar in my navigation bar, like safari has. If I add a UISearchBar to my view, it ends up underneath the navigation bar instead. How can I put a search bar in (not under) a unnavigationbar?

Upvotes: 0

Views: 378

Answers (3)

Fran Sevillano
Fran Sevillano

Reputation: 8163

I do it this way:

UISearchBar * navSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 232, 44)];
navSearchBar.delegate = self;
self.navigationItem.titleView = navSearchBar;   
[navSearchBar release];

I hope it helps!

Upvotes: 1

Mohammad
Mohammad

Reputation: 1666

Try putting UISearchBar or UITextField in titleView of UINavigationItem and then set this in topItem of navigation bar.

Refer to human interface guideline to confirm that it is allowed.

Upvotes: 2

adamsiton
adamsiton

Reputation: 3672

A navigationBar is mostly used to navigate a view hierarchy. If all you need is a search bar on the top of the page, why wont you just use a regular searchBar with a background similar to the navigationBar?

That way you will have much more control on the layout of the search bar, and you won't have to deal with the navigationBar.

By the way, this is probably the way the safari app actually work - it doesn't place the searchBar inside a navigationBar. You can see this by the fact that the searchBar is scrolled and doesn't stay on top.

Upvotes: -1

Related Questions