Search code examples
xcodeautolayout

Apply variations based on device's screen size in Xcode 8?


How do I apply auto layout variations based on the screen size of the selected device? All iPhone devices are Compact width Regular height, so if I want to add a variation for an iPhone 4s screen only, the variation gets applied to ALL iPhone screens since they're all part of the same trait. I remember this used to be pretty easy and straightforward in Xcode 7, but I'm guessing that this just another of millions things that Apple unnecessarily has to overcomplicate. How do I do this in Xcode 8?


Solution

  • How do I apply auto layout variations based on the screen size of the selected device

    In a nutshell: You don't. The idea of auto layout is that you apply constraints that cause your interface to look reasonably good in all the different iPhone sizes (that is, all the different ways that the trait collection can be Compact/Regular or Compact/Compact).

    This is typically done by using constraints that are based in some way on the size of the ultimate superview; thus, when the superview size is different (because you've launched on a different size screen), things still look good.

    I remember this used to be pretty easy and straightforward in Xcode 7

    Then you remember wrong. There is no difference in this regard between Xcode 7 and Xcode 8. There is a difference in how Xcode 8 displays to you the effect of your constraints in the Interface Builder canvas, but this is merely a matter of display in Interface Builder, and is in fact an improvement over Xcode 7 (which showed you a big unhelpful square).