在iOS 13上旋转设备时,我在UISearchController上遇到了一个问题。
我有一个tableView,这个tableView有一个searchBar as头。在iOS 13发布之前,这是正常工作的,现在旋转设备上有一个不想要的行为。
如果设备处于纵向并且searchBar处于对焦状态,则我将设备旋转到横向searchBar不在正确的位置;正如您在下面的图像上看到的那样,搜索栏一定是在黄色视图中,但它有点低于它
但是当按下cancel按钮时,搜索栏就会移到正确的位置。
我已经调试过了,视图似乎是在正确的位置创建的,包括帧大小和原点。
当焦点在搜索栏上,方向是横向,然后将其更改为纵向时,也会发生类似的行为。
添加搜索栏的代码:
func setupSearchBar() {
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.delegate = self
searchController.definesPresentationContext = true
searchController.searchBar.returnKeyType = UIReturnKeyType.done
searchController.searchBar.placeholder = "search here ..."
searchController.searchBar.tintColor = UIColor.red
searchController.searchBar.delegate = self
let yellowView: UIView = UIView.init(frame: searchController.searchBar.frame)
yellowView.backgroundColor = UIColor.yellow
yellowView.addSubview(searchController.searchBar)
tableView.tableHeaderView = yellowView
tableView.reloadData()
}
发布于 2019-10-03 03:05:51
我通过创建一个自定义的UISearchController解决了这个问题。
class CustomSearchController: UISearchController {
var _searchBar: UISearchBar = UISearchBar.init()
override var searchBar: UISearchBar {
get {
return _searchBar
}
set {
_searchBar = newValue
}
}
}
我没有使用UISearchController提供的默认UISearchBar,而是用我添加到tableView中的新UISearchBar覆盖了它。
func setupSearchBar() {
searchController.searchBar = searchBar
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.delegate = self
searchController.definesPresentationContext = true
searchController.searchBar.returnKeyType = UIReturnKeyType.done
searchController.searchBar.placeholder = "search here ..."
searchController.searchBar.tintColor = UIColor.red
searchController.searchBar.delegate = self
tableView.tableHeaderView = searchController.searchBar
}
此解决方案适用于iOS 9或更高版本,并保持应用程序以前的行为。
https://stackoverflow.com/questions/58191878
复制相似问题