My UIViewController creates its view by overwriting the loadView method:
- (void)loadView { UIView *view = [[UIView alloc] init]; view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; self.view = view; }
Now I would like to switch to AutoLayout and therefore add
view.translatesAutoresizingMaskIntoConstraints = NO;
to the loadView method. Now I have to specify the same restrictions that were previously auto-generated. My approach was to overwrite updateViewConstraints with
- (void)updateViewConstraints { if (0 == [[self.view constraints] count]) { NSDictionary* views = @{@"view" : self.view}; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:0 views:views]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:0 views:views]]; } [super updateViewConstraints]; }
But I get an exception because I think such restrictions should come with a super view:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That illegal.
So what should the correct Contraints look like?
source share