我想在点击后隐藏导航栏
navigationController?.hidesBarsOnTap = true
点击qazxsw poi后导航栏正常隐藏
但是在添加了一个searchController之后(下面的代码)
我的视图(青色)无法正确延伸
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
我也试过旋转它。出现搜索栏。
终于找到了解决方案
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.barHideOnTapGestureRecognizer.addTarget(self, action: #selector(barHideAction(_:)))
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
navigationController?.hidesBarsOnTap = true
}
@objc func barHideAction(_ guesture: UITapGestureRecognizer) {
updateFrame()
}
func updateFrame() {
if let nc = navigationController {
let isHidden = nc.isNavigationBarHidden
searchController.searchBar.superview?.isHidden = isHidden
if isHidden {
self.additionalSafeAreaInsets.top = -64 // fixed by a magic num
}
else {
self.additionalSafeAreaInsets.top = 0
}
}
}