我遇到一个问题,当我用自定义搜索栏推送新的UIViewController时,它会扩展标题栏的高度,但是当我关闭窗口时,我相信父UIViewController没有设法调整回标题条形图,因此它留下了一个黑色的间隙,如下图所示:
以下是我认为导致问题的自定义搜索栏的代码:(请注意,我知道在iOS 11中有一个新的搜索栏实现,但我试图向后兼容)。
let searchController = UISearchController(searchResultsController: nil)
func configureSearchController() {
// Initialize and perform a minimum configuration to the search controller.
// searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
searchController.searchBar.layer.cornerRadius = 15
searchController.searchBar.clipsToBounds = true
self.searchController.hidesNavigationBarDuringPresentation = false
self.extendedLayoutIncludesOpaqueBars = true
// Place the search bar view to the tableview headerview.
navigationItem.titleView = searchController.searchBar
}
为什么会发生这样的线索,我尝试打开标志来更新父导航控制器上的约束,但它仍然无法正常工作。如下所示:
override func didMove(toParentViewController parent: UIViewController?) {
if parent == nil {
print("parent moved back")
// self.parent?.navigationItem.titleView?.sizeToFit()
// self.parent?.navigationController?.navigationItem.titleView?.sizeToFit()
// self.parent?.navigationController?.navigationBar.setNeedsUpdateConstraints()
self.parent?.navigationItem.titleView?.setNeedsUpdateConstraints()
}
}