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?
问题:
回答1:
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!
回答2:
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.
回答3:
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.