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

San*_*eli 2 uitableview uikit ios uicollectionviewcell ios14

将手机更新到 iOS 14 后,我无法再与应用内UICollectionViewCell或UITableViewCell应用内的内容进行交互。该问题也存在于运行 iOS 14 的 iOS 模拟器中,但在 iOS 13 及更低版本上一切正常。

我在不再可交互的单元格中有按钮和其他集合视图。就好像整个单元格内容都变成了静态。下面是来自我UICollectionViewCells和的一些代码UICollectionViewController,但是同样的问题存在于UITableViewCells. 我正在使用 Swift 5.didSelectItemAt并且类似的功能工作正常,它在单元格内进行交互,但contentView似乎无法正常工作。如有必要,我可以粘贴更多代码,因为我修剪了一些绒毛和其他我缩小范围的代码,这不是问题。

由于这个问题存在于两者中UICollectionViewCell,UITableViewCell我相信我在更基本的层面上做错了。

相关代码来自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
}
Run Code Online (Sandbox Code Playgroud)

然后从相关代码 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
}
Run Code Online (Sandbox Code Playgroud)

编辑: 进一步检查视图层次结构后,UIView在 iOS 14 中单元格中的所有内容之上直接放置了一个附加视图。在 iOS 13 中,此视图不存在。下面附上图片。 iOS 13 符合预期 带有附加视图的 iOS 14

San*_*eli 6

感谢@Duncan C 推荐查看视图层次结构。

内容被简单地添加到单元格 addSubview()

相反,含量应加入到细胞是contentView用contentView.addSubview()。这解决了这个问题。

使用我当前的设置,在 iOS 13 及以下版本中,添加的内容只是addSubview()出现在单元格的contentView. 这在问题中显示的图片中得到了更好的描述。

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

  • 这真的很有帮助,过去三天一直在做。 (2认同)