iOS 11 SearchBar in NavigationBar
来源:互联网 发布:mac 设置airplay 编辑:程序博客网 时间:2024/06/18 06:38
https://stackoverflow.com/questions/45350035/ios-11-searchbar-in-navigationbar
With iOS 11 Apple has redesigned the UISearchBar by making the corners rounder and the height bigger. Adding a UISearchBar to the navigationBar is pretty simple by just setting it as the titleView of the navigationItem using navigationItem.titleView = searchBar
.
However, in iOS 11 it does not seem to work anymore as expected. Have a look at the screens where we compare the same setup using iOS 10 and iOS 11
iOS 10
iOS 11
You can clearly see that the SearchBar increases the size of the NavigationBar but the bar buttons do not get aligned correctly. Also the searchBar does not use the available space on the left anymore.
Putting the searchBar into a wrapper view to get the cancel button on iPad as described here Cancel button is not shown in UISearchBar also does not seem work anymore since the searchBar is then not visible at all.
If anyone has similar issues or already knowns how to fix/improve this I would be very thankful.
This was built using Xcode 9 Beta 4. Maybe future releases will fix this issue.
UPDATE:
Since this does not get "fixed" we now decided to use following solution: We added a new UIBarButtonItem in the NavBar and when the user presses this button we then present a new ViewController where we only put a searchBar into the NavBar which looks good now. Using the selected answer may be the best solution.
4 Answers
There's a new searchController property on navigationItem in iOS 11.
https://developer.apple.com/documentation/uikit/uinavigationitem/2897305-searchcontroller
Use like this...
if #available(iOS 11.0, *) { navigationItem.searchController = searchController} else { // Fallback on earlier versions navigationItem.titleView = searchController?.searchBar}
You can change the height of UISearchBar in iOS 11 by adding a constraint of height 44:
if #available(iOS 11.0, *) { searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true}
- iOS 11 SearchBar in NavigationBar
- searchbar隐藏navigationbar问题解决
- iOS自定义searchBar
- iOS自定义searchBar
- iOS之自定义searchBar
- iOS自定义SearchBar样式
- iOS searchBar底部黑线
- IOS-NavigationBar
- iOS-NavigationBar
- <iOS>NavigationBar
- 在navigationBar上添加searchBar 并设置字体
- IOS searchBar 修改背景颜色
- 关于IOS searchbar更改背景
- Xamarin IOS SearchBar 文本框设置
- SearchBar
- IOS-透明NavigationBar
- iOS 隐藏NavigationBar
- ios 的navigationBar
- Struts2拦截器总结学习
- 微服务:Eureka+Zuul+Ribbon+Feign+Hystrix构建微服务架构
- 终端
- Ubuntu16.04+Cuda8.0+OpenCV3.1.0+Caffe+Faster RCNN配置总结
- 浅谈HTML5离线缓存(Application Cache)
- iOS 11 SearchBar in NavigationBar
- Nginx+Tomcat搭建高性能负载均衡集群
- springboot入门之Thymeleaf篇
- erlang 使用】eredis 介绍
- leetcode_686.Repeated String Match ? 待解决
- 面试总结
- 【剩余类+树状数组】2017.10.23杂题(liu_runda NOIP 联考 DAY1)[飞]题解
- AppBarLayout
- 将url文件链接保存到本地服务器