Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on/off auto layout for a single view controller in storyboard

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?

like image 795
Mario Avatar asked Aug 31 '14 13:08

Mario


People also ask

What is auto layout?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.

How do I change the default view controller?

Open your storyboard. Click on the view controller corresponding to the view that you want to be the default view. Open the Attributes Inspector. Check the Is Initial View Controller check box in the View Controller section.


1 Answers

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.

like image 157
Rob Avatar answered Sep 30 '22 17:09

Rob