ios - 允许在 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
我很感激解决这个问题的任何帮助:)
解决方案
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
调用下方解决了我的问题。
希望这对碰巧遇到类似问题的其他人有所帮助!
推荐阅读
- c++ - 如何实例化一个长度使用模板函数中的模板参数的数组
- reactjs - react-admin 中 MenuItemLink 的 primaryText 没有翻译
- git - 平分错误提交
- windows - CryptProtectData 函数的 CRYPTPROTECT 标志
- node.js - 如何将 TVP 中的缓冲区数据从 node.js 传递到 lambda 中的 sql server?
- powershell - 使用powershell变量在变量中的文件路径执行程序
- php - 从第二个数组中获取数据并将值放入数组 1
- javascript - 为什么打印出使用 JavaScript 的 `Array.sort` 排序的数组似乎在调用 `Array.sort` 之前对数组进行了排序?
- php - 搜索两个过滤器匹配 Elasticsearch
- angular - 无法绑定到“显示”,因为它不是