Search code examples
iosswiftxcodestoryboard

Quick way to make all active constraints only apply to 1 size class


The situation is an iPhone app that now needs to be responsive on iPad (only in portrait mode). I don't want to affect the constraints at all on iPhone. I understand the idea of size classes, so all iPhones will be compact width/regular height, whereas all iPads will be regular width/regular height, which will allow a nice separation. I will be using the storyboard interface builder.

My question: I'm wondering if there's a quicker way than going through all the constraints one by one and changing the class in the constraints sidebar to be make them only affect cW-rH. This is something I've done multiple times now on different views so I'm really hoping there's a way to streamline this process. It's also a process that's very prone to errors, and when I accidently remove a constraint, I have to restart because I can't figure out which one was the one I shouldn't have removed.

Thanks!


Solution

  • Please note - My answer pertains to the use of UIKit. I am unaware if it also applies to SwiftUI.

    If you want to keep everything inside of the Storyboard then you're already doing the most that you can do; there is nothing else.

    If you're open to doing some of the work programmatically; however, you can take a hybrid approach.

    STEP 1:

    To start, add UI objects to the storyboard & create IBOutlets as usual.

    STEP 2:

    Then, programmatically check the device's width & height.

    STEP 3:

    Then, programmatically set the sizes of each UI object's leading, trailing, top & bottom constraints. Use the device's width & height that you acquired in step 2 & use them to determine what you want to set the constraints' constant to.