如何将搜索栏放在非导航栏中(而不是下方)?
我想在导航栏中放置一个搜索栏,就像 safari 那样。如果我将 UISearchBar 添加到视图中,它最终会出现在导航栏下方。如何将搜索栏放在非导航栏中(而不是下方)?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将 UISearchBar 或 UITextField 放在 UINavigationItem 的 titleView 中,然后将其设置在导航栏的 topItem 中。
请参阅人机界面指南确认是否允许。
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.
我这样做:
我希望它有帮助!
I do it this way:
I hope it helps!
导航栏主要用于导航视图层次结构。如果您需要的只是页面顶部的搜索栏,为什么不使用背景与导航栏类似的常规搜索栏呢?
这样您就可以更好地控制搜索栏的布局,并且不必处理导航栏。
顺便说一句,这可能是 safari 应用程序实际工作的方式 - 它不会将搜索栏放在导航栏内。您可以通过以下事实看出这一点:searchBar 是滚动的并且没有停留在顶部。
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.