iosautolayoutnslayoutconstraintuistackviewsize-classes

iOS autolayout constraint violation when device orientation is changed


I have an autolayout layout that has been created for an iPhone in portrait mode.

When I rotate the device from portrait to landscape I get a warning saying the system cannot satisfy two constraints at the same time. This happens when rotating to and from both orientation.

enter image description here

I believe I have found the offending constraint (highlighted below), and it is one generated by iOS, moving my constraint that is in conflict breaks my layout and causes more constraint violation errors.

The layout works visually; so long as I ignore the warning.

The layout consists of two basic parts, a control parts (using a UIStackView that is 30% of the height of the device) and a UITableView that fills the remaining area. These are embedded in a UIStackView that is set to cover the default UIView with margins to the safe area.

enter image description here

To make best use of the the available screen space when the device is rotated to landscape the control part should go from above the UITableView to beside it, and take up 45% of the width of the screen.

The conflicting constraint is on the "controller" part of the view setting its leading edge to be 16 points from the superviews leading edge.

Is there away to get around this? I don't like too, but can the warning be ignored?

I have tried embedding the main UIStackView in a UIView and removing the constraint that is in conflict; but his causes other issues unless I add in constraints between the UIView and UIStackView which then have the same conflicting constraints issue.

A repo for an example project with the same issue can be found at.

When simplifying the layout it appears I am unable to have a UIStackView with Alignment = Fill, Distribution = Fill Equally and Spacing = 5 (or standard spacing) as the spacing on the UIStackView appears to be. in conflict.


Solution

  • It's common to see these types of constraint warnings when using Vary for Traits.

    You can get rid of the constraint conflicts on rotation by changing the Priority on both of your proportional constraints on Info Stack View to 999.

    When I run your Experiment app, I also see a constraint conflict on your Detail Stack View ... but, it looks like an internal conflict that you may not be able to get rid of. Changing that stack view's Distribution to Fill Equally instead of Fill Proportionally clears it, but that may not give you the exact layout you're looking for.

    Fill Proportionally seems to be the most misunderstood and misused setting... I'd suggest modifying your layout to avoid that... Probably by embedding Detail Stack View in a UIView, Distribution: Fill, and constraining it to be centered in that UIView.