首页 > 解决方案 > 允许在 UISearchController 处于活动状态时选择单元格

问题描述

我有UICollectionViewController一个UISearchController嵌入在 parentUINavigationController的导航栏中。我正在使用作为自定义子类的单元格,其中UICollectionViewCell包含单个UIImageView.

但是,在搜索时,我无法选择通过搜索找到的单元格。当搜索控制器未激活时,选择/点击单元格可以完美地工作,我已经尝试过这个答案但没有成功。

这是我的搜索设置代码:

searchController.searchResultsUpdater = self
searchController.definesPresentationContext = true
searchController.searchBar.keyboardAppearance = .default
searchController.obscuresBackgroundDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false

navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false

我很感激解决这个问题的任何帮助:)

标签: iosswiftuicollectionviewuikit

解决方案


UICollectionView在玩了一会儿之后想通了。

我正在覆盖touchesBegan,如下所示:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard !searchController.isActive else { return }
    super.touchesBegan(touches, with: event)
    UIView.animate(withDuration: 0.1) { () -> Void in
        let shrinkTransform = CGAffineTransform.init(scaleX: 0.9, y: 0.9)
        self.transform = shrinkTransform
    }
}

问题是我在调用之前检查搜索控制器是否处于活动状态super.touchesBegan,只要搜索控制器处于活动状态,就会阻止触摸。

将保护语句直接移动到super.touchesBegan调用下方解决了我的问题。

希望这对碰巧遇到类似问题的其他人有所帮助!


推荐阅读