问题描述
在iOS 11以下的版本上,当点击输入框进行搜索,或者不搜索直接点击取消后,上滑页面,搜索框不能正确的弹回,被覆盖在导航栏下面;
问题代码
override func viewDidLoad() {
super.viewDidLoad()
//配置搜索控制器
uiSearchController = UISearchController(searchResultsController: nil)
uiSearchController.searchResultsUpdater = self
uiSearchController.searchBar.placeholder = "请输入名称进行搜索"
uiSearchController.searchBar.searchBarStyle = .minimal
uiSearchController.searchBar.sizeToFit()
uiSearchController.searchBar.returnKeyType = .done
uiSearchController.hidesNavigationBarDuringPresentation = false
uiSearchController.dimsBackgroundDuringPresentation = false
uiSearchController.searchBar.enablesReturnKeyAutomatically = false
if #available(iOS 11.0, *){
navigationItem.searchController = uiSearchController
navigationItem.hidesSearchBarWhenScrolling = false
}else{
self.tableView.tableHeaderView = uiSearchController.searchBar
self.automaticallyAdjustsScrollViewInsets = false
}
}
正确代码
override func viewDidLoad() {
super.viewDidLoad()
//配置搜索控制器
uiSearchController = UISearchController(searchResultsController: nil)
uiSearchController.searchResultsUpdater = self
uiSearchController.searchBar.placeholder = "请输入名称进行搜索"
uiSearchController.searchBar.searchBarStyle = .minimal
uiSearchController.searchBar.sizeToFit()
uiSearchController.searchBar.returnKeyType = .done
uiSearchController.hidesNavigationBarDuringPresentation = false
uiSearchController.dimsBackgroundDuringPresentation = false
uiSearchController.searchBar.enablesReturnKeyAutomatically = false
if #available(iOS 11.0, *){
navigationItem.searchController = uiSearchController
navigationItem.hidesSearchBarWhenScrolling = false
}else{
let uiView:UIView = UIView(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 45))
uiView.addSubview(uiSearchController.searchBar)
self.tableView.tableHeaderView = uiView
self.automaticallyAdjustsScrollViewInsets = false
}
}
下载的官方示例中也存在这个问题,可能一直没有修复;
改正的具体原理不清楚,只是在外面再包了一层view。
网友评论