Home > front end >  Programmatically move contraints - Xcode reports error on not existing constraint
Programmatically move contraints - Xcode reports error on not existing constraint

Time:12-22

I have create a enter image description here

When I create a new, empty User Interface file (a .xib) and drag a collection view cell into it, it starts like this:

enter image description here

Then, after adding a label:

enter image description here

Checking the xml source, TestCell.xib shows:

<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">

where it should be:

<collectionViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" id="y54-cV-vD7">

So, somehow, your contentView was changed to a default UIView. I can't find the docs right now, but I know I've come across similar issues before where changing the Class of the content view causes all sorts of problems.

I can't even get it to change in IB, so maybe Xcode no longer allows it to avoid the issue.


Edit - couple notes...

First comment: your TestCell.xib has a second Bottom constraint. Nothing wrong with that, as there are many reasons to have multiple "same item" constraints with different Priorities, for example.

In this case, that constraint is not Installed, so we wouldn't expect it to affect anything.

However, if I mark it as a Placeholder ☑️ Remove at build time, or if I delete it, no more constraint conflicts.

I suspect something in your moveSubviewsIntoView() func is copying that not-installed constraint and activating it.

Second comment - about the Content View:

If I create an Empty xib, and drag a UICollectionViewCell into it, IB automatically gives it the expected Content View.

However, if I go New File -> Cocoa Touch Class -> Subclass of: UICollectionViewCell and I check ☑️ Also create XIB file, IB does NOT give me the Content View.

Surprisingly to me, that does not cause a problem! Everything I read in Apple's docs (and elsewhere) reinforces the instruction to only add subviews to the cell's Content View (same with table view cell). So, it seems really odd... Is that a "bug"? I'm not an Apple engineer, so I don't know - maybe I'm just missing some information (very possible or even likely the case).

  • Related