Search code examples
objective-cinterface-builderxcode8size-classes

XCode8: UICollectionView edge-to-edge, Editing constraints in One Trait Variation (Size Class) breaks other Trait Variations


I am having an infuriating issue with Xcode interface builder. I have a storyboard where one view controller has a UICollectionView that's edge to edge to the View Controller's Root View. The root View has Descendant constraints of -16 and -16 for Leading and Trailing space to the UICollectionView.

When I switch to the other size class (Or Trait Variation as it's now called), the Leading and Trailing margins become -20, -20. If I adjust them to be edge to edge, then the other Trait Variation becomes inset by 4 pixels on each side. So editing the constraints in one variation screws up the other variation, and thus all my internal calculation logic is off for the cells.

So i can not simultaneously satisfy the constraints in multiple trait variations all at the same time, and have my UICollectionView just be edge-to-edge without any padding around it.

I tried remaking all constraints. Same issue.

I tried turning off Trait Variations on the storyboard itself. Same issue.

EDIT: Also tried adding an "Intermediary View" (also edge-to-edge) and housing my UICollectionView inside it. Makes no difference.

I noticed that the negative inset amount has to be different on each device factor. On some it's -16 -16, on some it's -20, -20, on iPad it's -8, -8. So since these are static values, then how can you embed a scrollview edge to edge reliably, without fiddling with the constraints programmatically at runtime?

I can fix one device screen size, but it breaks the other screen sizes.

Also, i don't actually want to use size classes, but even if i turn off size classes, i still don't get one unified view for editing the constraints in one place. So this effectively multiplies the work i have to do by 6 times. This is costing me a ridiculous amount of time already.

Any suggestions?


Solution

  • I found a cheap and easy solution. My problem was: my Leading and Trailing constraints were to the Margin, and not to the superview directly. The margin is what differs across size classes. So I modified my 2 constraints: Leading and Trailing, disabling the "Relative to Margin" option on each. Instructions here: UIViewController Nested View To Screen Edge

    Now i get a consistent behaviour on every screen size (no margins around collection view).