How to Enable / Disable Auto Layout for a Single View Controller in the Timeline

advertisements

I want to turn on/off auto layout for a single view controller in storyboard (called scene I believe)

When I go to a specific scene in storyboard, and change the Use Auto Layout checkbox in the inspector, I get the auto layout on/off for the entire storyboard scenes.

I can't understand this behaviour, because for one this checkbox looks like a property of a specific scene and not the entire storyboard, and second I don't see why one scene layout has something to do with the other scenes.

Is this even possible? Is it possible in storyboard or maybe only in code?


It may look like this "Use Auto Layout" checkbox is for a specific scene, but as you've discovered, it's for the entire "Interface Builder Document" (i.e., the entire storyboard). Thus, if you don't want to use auto layout on a particular scene, you are stuck with a few alternatives:

  1. You can either put this other scene in a different storyboard or NIB and transition to and from view controller programmatically. You lose many of the storyboard benefits if you do that (e.g. you cannot just create segues back and forth in IB, but rather have to transition to and from this scene programmatically).

  2. You could keep auto layout enabled but then for the one view controller in question, you can programmatically:

    • Remove any constraints that IB may have added (esp in Xcode 6);

    • Adjust the autoresizingMask for your various controls. If you want your autoresizingMask to be honored, you may want to turn on translatesAutoresizingMaskIntoConstraints; and

    • If using Xcode 6, you might want to turn off "Use Size Classes", too, so that you're laying out the scene properly for the target device.

    So, having laid out my label the way I wanted in IB, I could then:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self.view removeConstraints:self.view.constraints];
        [self.label removeConstraints:self.label.constraints];
        self.label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
        self.label.translatesAutoresizingMaskIntoConstraints = YES;
    }
    
    

    Clearly, what you set the autoresizingMask to is entirely up to you (in this example, I put a label centered on scene, so the above keeps it centered as I go to landscape), but hopefully this illustrates the idea.

Frankly, given that there's nothing you can do with autosizing masks that you can't easily express in auto layout, I'd be inclined to stick with auto layout, and you eliminate the awkwardness of the above approaches.