【发布时间】:2020-03-01 22:18:32
【问题描述】:
来自 Wenderlich 的 Candy Search 克隆。我添加了一个图标来显示/隐藏搜索栏。
但是,从 navigationItem 中移除搜索控制器后,导航栏不会恢复到原来的大小。
有谁知道如何做到这一点?我尝试了几件事,但都没有奏效。
self.searchController.isActive = false 没有任何区别。
还有一件事,在这令人沮丧的时候,如果您现在单击一行以实例化一个细节控制器并返回,导航栏将恢复到正常高度!
谢谢!
看截图:
完整项目在这里:https://github.com/HerrDoktorBD/CVSearch
相关代码:
lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candy"
// scope bar
searchController.searchBar.scopeButtonTitles = [
"All",
"Chocolate",
"Hard",
"Other"
]
return searchController
}()
func showSearchBar(show: Bool) {
let sb: UISearchBar = searchController.searchBar
if show {
//print("show searchbar")
UIView.animate(withDuration: 0.3,
delay: 0.0,
options: .curveEaseOut,
animations: {
if #available(iOS 13, *) {
self.navigationItem.searchController = self.searchController
}
}, completion: { (status) in
sb.becomeFirstResponder()
})
}
else {
//print("hide searchbar")
UIView.animate(withDuration: 0.3,
delay: 0.0,
options: .curveEaseOut,
animations: {
sb.resignFirstResponder()
}, completion: { (status) in
if #available(iOS 13, *) {
self.navigationItem.searchController = nil
}
})
}
}
【问题讨论】:
-
您是否尝试过更新布局。 [self.view.layoutIfNeeded()]
-
感谢您的建议,但没有帮助。我还尝试了
self.view.setNeedsLayout(),它使tableview 向上滑动,但navigationItem 高度仍然不会重置。我们越来越近了…… -
我在另一个堆栈帖子上找到了这个。 navigationController?.view.setNeedsLayout() navigationController?.view.layoutIfNeeded() m8 工作检查这个! stackoverflow.com/questions/46318022/…
-
那行得通。我不得不用
self.perform(#selector(self.forceNavHeightReset), with: nil, afterDelay: 0.1)打电话给@objc func forceNavHeightReset() { self.navigationController?.view.setNeedsLayout() self.navigationController?.view.layoutIfNeeded() }。更新了 github 仓库。非常感谢。 -
我可以添加这个作为答案你会投票吗?
标签: swift uinavigationcontroller uisearchbar uisearchcontroller