Home > Software engineering >  Xcode autolayout constraint landscape orientation issue where view shrinks to zero size
Xcode autolayout constraint landscape orientation issue where view shrinks to zero size

Time:02-02

I have a single view on screen that is constrained in the center x and y positions. It also has an aspect ratio to itself. The problem comes with the next constraints.

In portrait mode (compact width, regular height) it has a proportional width constraint to superview = 0.8 and this constraint is uninstalled in landscape mode.

In landscape mode, it has a proportional width constraint to superview = 0.4 and this constraint is uninstalled in portrait mode.

In the storyboard when in flip the device between portrait and landscape there's no problem and no auto layout issues, and the red view resizes according to the constraints, but in the simulated device and in my iPhone when I change it to landscape the view shrinks to zero size. I can't figure out why this is happening. Am I missing something? enter image description hereenter image description here

CodePudding user response:

Different devices have different size classes.

For example, an iPhone 13 Pro Max uses:

wC hR   // portrait orientation
wR hC   // landscape orientation

whereas an iPhone 8 uses:

wC hR   // portrait orientation
wC hC   // landscape orientation

Note that landscape is wC hC, not wR hC.

Change your "landscape" variation to Width: Any Height: Compact

enter image description here

See the Apple docs here for a list of devices and size classes.

  •  Tags:  
  • Related