【发布时间】:2020-02-25 18:32:46
【问题描述】:
我在模态视图控制器中使用 UISearchController 搜索栏作为导航栏的标题视图。我是这样设置的:
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.showsCancelButton = false
navigationItem.titleView = searchController.searchBar
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// for whatever reason, it's necessary to make the search controller first responder
// on the main queue. reference: https://stackoverflow.com/a/41657181/2335677
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
一切都很好,除了dismiss() 和滑动关闭模式在searchController.isActive = true 时不起作用
我可以通过首先将其设置为非活动来解决dismiss() 问题:
@IBAction private func done(_ sender: UIBarButtonItem) {
searchController.isActive = false
dismiss(animated: true)
}
但是我不能像我提到的那样向下滑动来关闭视图控制器。而且我想不出解决方法。我试过了:
- 设置 isModalInPresentation = false(不是一个选项,因为我的应用目标是 iOS12)
- 使用
UIAdaptivePresentationControllerDelegate方法并尝试在其中一些方法中设置searchController.isActive = false(不起作用) - 设置
definesPresentationContext = true(没有区别)
还有人有其他想法吗?
【问题讨论】: