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

故事扮演 提交于 2019-12-12 01:26:37

问题


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.



来源:https://stackoverflow.com/questions/7298965/how-can-i-put-a-search-bar-in-not-under-a-unnavigationbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!