【问题标题】:Dismiss and swipe to dismiss issues with search controller active关闭并滑动以关闭搜索控制器处于活动状态的问题
【发布时间】: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)
}

但是我不能像我提到的那样向下滑动来关闭视图控制器。而且我想不出解决方法。我试过了:

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

还有人有其他想法吗?

【问题讨论】:

    标签: uisearchcontroller swift5


    【解决方案1】:

    如果您想要一个单独的屏幕仅用于搜索,并且您只需要像我一样在导航栏标题中添加一个搜索栏,那么使用 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 没有劫持你的视图控制器。

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多