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.

  • ViewController gets deallocated which leads to crash
  • Load an UIView from UIViewController inside storyboard doesn't work
  • Swipe to go back only works on edge of screen?
  • Can't submit the app with storyboard launch images to the App Store: Your binary is not optimized for iPhone 5
  • UICollectionView distance between cells?
  • How to Send data between view controllers StoryBoard Xcode
  • 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?

    2 Solutions Collect From Internet About “How to turn on/off auto layout for a single view controller in storyboard”

    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.

    You can move your single view controller’s layout to a separate xib file and load it programmatically. And if you simply don’t want to define your own constraints, you can leave your controller in the storyboard and simply not add constraints to it. In that case, constraints will be automatically generated for you at runtime, based on the VC’s child views’ frames.