ios - 无法再与 iOS 14 中的 UICollectionViewCell 或 UITableViewCell 中的内容交互

标签 ios uitableview uikit uicollectionviewcell ios14

<分区>

将我的手机更新到 iOS 14 后,我无法再与我的应用程序的 UICollectionViewCellUITableViewCell 中的内容进行交互。运行 iOS 14 的 iOS 模拟器中也存在此问题,但在 iOS 13 及以下版本中一切正常。

我的单元格中有按钮和其他 Collection View ,它们不再可交互。就好像整个单元格内容都变成了静态一样。下面是我的 UICollectionViewCellsUICollectionViewController 中的一些代码,但是 UITableViewCells 中也存在同样的问题。我正在使用 Swift 5。didSelectItemAt 和类似的功能工作正常,它在单元格的 contentView 中交互,特别是似乎不起作用。如有必要,我可以粘贴更多代码,因为我修剪了一些绒毛和其他我已缩小范围以不成问题的代码。

由于 UICollectionViewCellUITableViewCell 都存在这个问题,我相信我在更基本的层面上做错了。

UICollectionViewCell中的相关代码:

override init(frame: CGRect) {
    super.init(frame: frame)
    setupContainers()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupContainers() {
    // Adding subviews like so...
    addSubview(subviewToAdd)

    // Anchor all subviews using NSConstraints
}

然后是UICollectionViewController中的相关代码

override func viewDidLoad() {
    super.viewDidLoad()
    setStyleAndDelegates()
}

private func setStyleAndDelegates() {
    collectionView?.backgroundColor = UIColor.white

    collectionView?.delegate = self
    collectionView?.dataSource = self
    
    collectionView?.register(MyCell.self, forCellWithReuseIdentifier: eventCellId)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: eventCellId, for: indexPath) as! MyCell
    return cell
}

编辑: 进一步检查 View 层次结构后,在 iOS 14 中,有一个额外的 UIView 直接放置在单元格中的所有内容之上。在 iOS 13 中,此 View 不存在。下面附上图片。 iOS 13 as expected iOS 14 with additional view

最佳答案

感谢@Duncan C 建议查看 View 层次结构。

内容只是通过 addSubview()

添加到单元格中

相反,应该使用 contentView.addSubview() 将内容添加到单元格的 contentView。这解决了问题。

使用我当前的设置,在 iOS 13 及以下版本中,仅使用 addSubview() 添加的内容出现在单元格的 contentView 上方。问题中显示的图片更好地描绘了这一点。

在 iOS 14 和我当前的设置中,此内容被插入到单元格的 contentView 下方,因此 contentView 阻止了用户与其余部分的交互内容直接添加到单元格。

关于ios - 无法再与 iOS 14 中的 UICollectionViewCell 或 UITableViewCell 中的内容交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63960541/

相关文章:

iphone - 在 iPhone 上使用 OpenGL 的分层 2D 应用程序最有效的 "architecture"?

ios - UItabbar 项目不显示 Storyboard 引用

cocoa-touch - 旋转一些 UIView,但不是全部?

ios - 程序化嵌入转场

ios - UITableView deleterows 滚动到顶部

iphone - 动态加载分组 TableView

ios - 使用 Swift 进行 TableViewCell 委托(delegate)

ios - 远程通知中的自定义声音 iOS 10,swift 3

ios - 分发内部业务 IOS 应用程序

ios - 如何在 iOS 6 中禁用 UITableView 水平滚动?