I am developing an application in which i am using auto layout. I am following the following steps :
step 1 : create a button in
viewDidLoad
[super viewDidLoad]; self.view.translatesAutoresizingMaskIntoConstraints = NO; _button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; _button1.translatesAutoresizingMaskIntoConstraints = NO; [_button1 setTitle:@"B" forState:UIControlStateNormal]; [self.view addSubview:_button1];
step 2 : implement constraints in
updateViewConstraints
method[super updateViewConstraints]; [self.view removeConstraints:self.view.constraints]; if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) { NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:100.0f]; [self.view addConstraint:constraint]; NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-100.0f]; [self.view addConstraint:constraint1]; NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:200.0f]; [self.view addConstraint:constraint2]; NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-100.0f]; [self.view addConstraint:constraint3]; _button1.backgroundColor = [UIColor redColor]; } else{ NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:200.0f]; [self.view addConstraint:constraint]; NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-200]; [self.view addConstraint:constraint1]; NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:50.0f]; [self.view addConstraint:constraint2]; NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-50.0f]; [self.view addConstraint:constraint3]; _button1.backgroundColor = [UIColor blueColor]; }
but when i switch the device orientation, the console prints the following :
Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "UIView:0x8a461c0 (Names: '|':UIWindow:0x8a42970 )>", "", "", "UIButton:0x8a45ea0 (Names: '|':UIView:0x8a461c0 )>", "" )
Will attempt to recover by breaking constraint
Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
could anyone please tell me what is wrong with this layout ?
The issue is that you're calling
[super updateViewConstraints]
inupdateViewConstraints
while you still have constraints in place for the button. So, as you transition from landscape to portrait, you still have the landscape button constraints (which are unsatisfiable in portrait), but are asking the main view to update its (portrait) constraints. If you move the call to[super updateViewConstraints]
anywhere after you remove all of your existing button constraints, and you should be in good shape.A couple of asides:
If using storyboards/NIBS, you should remove the line that says:
But keep the line that says:
I'd be wary of a wholesale removal of all constraints. I usually keep arrays of the constraints I want to remove, and that way I can easily remove just the ones that I need removing and will be reconstructing. In your case, removing all is probably fine, but as you add more and more constraints to your view, it's probably just easier to keep track of which you want to remove and reconstruct:
I might suggest using VFL, which is a little more concise: