导航栏中的UISearchController无法在iOS 11上正确隐藏

问题描述 投票:1回答:1

我想在点击后隐藏导航栏

navigationController?.hidesBarsOnTap = true

点击qazxsw poi后导航栏正常隐藏

但是在添加了一个searchController之后(下面的代码)

hide Navigation Bar 

我的视图(青色)无法正确延伸

let searchController = UISearchController(searchResultsController: nil) navigationItem.searchController = searchController

我也试过旋转它。出现搜索栏。

not properly

ios uinavigationbar ios11 uisearchcontroller abcustomuinavcontroller
1个回答
0
投票

终于找到了解决方案

enter image description here

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 } } }

© www.soinside.com 2019 - 2024. All rights reserved.