gpt4 book ai didi

uisearchcontroller - 关闭并滑动以关闭搜索 Controller 处于事件状态的问题

转载 作者:行者123 更新时间:2023-12-05 07:12:34 31 4
gpt4 key购买 nike

我在模态视图 Controller 中使用 UISearchController 搜索栏作为导航栏的标题 View 。我是这样设置的:

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

但我不能像我提到的那样向下滑动以关闭 View Controller 。而且我想不出解决方法。我试过:

  1. 设置 isModalInPresentation = false(不是一个选项,因为我的应用目标是 iOS12)
  2. 尝试使用 UIAdaptivePresentationControllerDelegate 方法并尝试在其中一些方法中设置 searchController.isActive = false(无效)
  3. 设置 definesPresentationContext = true(没有区别)

还有人有其他想法吗?

最佳答案

如果您只需要一个单独的屏幕来进行搜索,并且您只需要像我一样在导航栏标题中添加一个搜索栏,那么使用 UISearchBar 会更容易:

var searchBar: UISearchBar!

override func viewDidLoad() {
super.viewDidLoad()
searchBar = UISearchBar()
searchBar.showsCancelButton = false
navigationItem.titleView = searchBar
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchBar.becomeFirstResponder()
}

它看起来一样,你现在得到所有默认行为,因为 UISearchController 没有劫持你的 View Controller 。

关于uisearchcontroller - 关闭并滑动以关闭搜索 Controller 处于事件状态的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60401111/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com