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

2019-09-20 00:02发布

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?

3条回答
ゆ 、 Hurt°
2楼-- · 2019-09-20 00:12

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-09-20 00:21

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!

查看更多
劳资没心,怎么记你
4楼-- · 2019-09-20 00:31

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.

查看更多
登录 后发表回答