Can no longer interact with content within UICollectionViewCell or UITableViewCell in iOS 14

Viewed 1095

After updating my phone to iOS 14, I can no longer interact with content within UICollectionViewCell or within UITableViewCell for my app. The issue is also present in iOS simulators running iOS 14, but everything works fine with iOS 13 and below.

I have buttons and other collection views within the cells that are no longer interactable. It's as if the entire cell content has become static. Below is some code from my UICollectionViewCells and UICollectionViewController, however the same issue is present in UITableViewCells. I am using Swift 5. didSelectItemAt and similar functions work fine, it's interacting within the cell's contentView specifically that appears to not be working. I can paste more code if necessary as I trimmed some fluff and other code I have narrowed down to not be a problem.

Since the issue is present in both UICollectionViewCell and UITableViewCell I believe I am doing something wrong at a more fundamental level.

Relevant code from the 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
}

And then relevant code from the 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
}

EDIT: Upon further inspection of the view hierarchy, there is an additional UIView placed directly on top of all content in the cell in iOS 14. In iOS 13, this view is not there. Pictures attached below. iOS 13 as expected iOS 14 with additional view

1 Answers

Thank you to @Duncan C for recommending taking a look at the view hierarchy.

Content was being added to cell simply by addSubview()

Instead, content should be added to the cell's contentView with contentView.addSubview(). This fixed the issue.

With my current setup, in iOS 13 and below content added simply with addSubview() appeared above the cell's contentView. This is better portrayed in the pictures shown in the question.

In iOS 14 and again with my current setup, this content was getting inserted below the cell's contentView, and as such the contentView was blocking user interaction to the rest of the content added directly to the cell.

Related