Autolayout issue

rokridi

I am developing an application in which i am using auto layout. I am following the following steps :

  1. 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];
    
  2. 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 ?

Rob

The issue is that you're calling [super updateViewConstraints] in updateViewConstraints 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:

  1. If using storyboards/NIBS, you should remove the line that says:

    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    

    But keep the line that says:

    _button1.translatesAutoresizingMaskIntoConstraints = NO;
    
  2. 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:

    @property (nonatomic, strong) NSArray *verticalConstraints;
    @property (nonatomic, strong) NSArray *horizontalConstraints;
    
  3. I might suggest using VFL, which is a little more concise:

    - (void)updateViewConstraints
    {
        if (self.horizontalConstraints)
            [self.view removeConstraints:self.horizontalConstraints];
    
        if (self.verticalConstraints)
            [self.view removeConstraints:self.verticalConstraints];
    
        [super updateViewConstraints];
    
        NSDictionary *views   = NSDictionaryOfVariableBindings(_button1);            
        NSDictionary *metrics = nil;
    
        if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
        {
            metrics = @{@"left"   : @100,
                        @"right"  : @100,
                        @"top"    : @200,
                        @"bottom" : @100};
    
            _button1.backgroundColor = [UIColor redColor];
        } else{
            metrics = @{@"left"   : @200,
                        @"right"  : @200,
                        @"top"    : @50,
                        @"bottom" : @50};
    
            _button1.backgroundColor = [UIColor blueColor];
        }
    
        self.horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(left)-[_button1]-(right)-|" options:0 metrics:metrics views:views];
        self.verticalConstraints   = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(top)-[_button1]-(bottom)-|" options:0 metrics:metrics views:views];
        [self.view addConstraints:self.horizontalConstraints];
        [self.view addConstraints:self.verticalConstraints];
    }
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Autolayout issue

From Dev

Tableheader view autolayout issue

From Dev

Autolayout issue with UIWebView

From Dev

UIScrollview scrolling issue with autolayout

From Dev

Autolayout issue in iOS

From Dev

Autolayout in code ? height issue?

From Dev

Autolayout Top space issue

From Dev

Uiimageview autolayout issue

From Dev

Autolayout alignment issue

From Dev

Autolayout issue AlignParentBottom

From Dev

AutoLayout issue with TabBarController

From Dev

AutoLayout constraint issue with unexpected NSAutoresizingMaskLayoutConstraint

From Dev

ScrollView issue on AutoLayout ( Xcode 6)

From Dev

Ios horizontal scrollview and autolayout issue

From Dev

ScrollView issue on AutoLayout ( Xcode 6)

From Dev

Ios horizontal scrollview and autolayout issue

From Dev

AutoLayout issue in dynamic UITableViewCell's subviews

From Dev

TableView-keyboard animation issue with autolayout

From Dev

Autolayout issue for width equally in iOS iPhone

From Dev

Xcode 6.1 Autolayout issue for 3.5in display

From Dev

Xcode 6.1 Autolayout issue for 3.5in display

From Dev

AutoLayout TableView issue with iPhone 6 and 6 Plus

From Dev

iOS autolayout vs ScrollView : Contentsize issue

From Dev

iOS: Autolayout and popOverController issue (possible Xcode bug)

From Dev

uitableviewcell uiimage autolayout issue in 4 inch iphone

From Dev

Autolayout issue with multiline views in two stack views

From Dev

How to resolve Autolayout issue in Swift 3?

From Dev

Custom TableViewCell autolayout issue upon orientation change

From Dev

pushViewController cause autoLayout issue using hidesBottomBarWhenPushed (iphone6)