0
votes

I need to change a view's height when rotating the device. I'm using storyboard with autolayout, and I have the height constraint set as an IBOutlet in the view controller:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *passwordViewHeight;

Then, in the view controller I also have the methods:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
   [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

        [self.view setNeedsUpdateConstraints];

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {

    }];

   [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

- (void)updateViewConstraints
{
   [super updateViewConstraints];

   UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

   if (orientation == UIInterfaceOrientationPortrait) {
       [self.welcomeLabel setHidden:NO];
       [self setAllPortraitConstraints];
   }
   else if ((orientation == UIInterfaceOrientationLandscapeLeft) ||
            (orientation == UIInterfaceOrientationLandscapeRight)) {
       [self.welcomeLabel setHidden:YES];
       [self setAllLandscapeConstraints];
   }
}

- (void)setAllLandscapeConstraints
{
   [self.view removeConstraint:self.passwordViewHeight];
   // More constraints

   self.passwordViewHeight = [NSLayoutConstraint constraintWithItem:self.passwordView
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0
                                                           constant:34.0];

   // More constraints

   [self.view addConstraint:self.passwordViewHeight];
   // More constraints
}

- (void)setAllPortraitConstraints
{
   [self.view removeConstraint:self.passwordViewHeight];
   // More constraints

   self.passwordViewHeight = [NSLayoutConstraint constraintWithItem:self.passwordView
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0
                                                           constant:40.0];

   // More constraints

   [self.view addConstraint:self.passwordViewHeight];
   // More constraints
}

Then, when I run the app in portrait orientation and I rotate the device to landscape, I get this log in Xcode:

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)

(
   "id: login.1, constant: 40.000000",
   "id: (null), constant: 34.000000"
)

Will attempt to recover by breaking constraint id: login.1, constant: 40.000000

I want to substitute the constraint to give it a new constant when device orientation changes, what I'm doing wrong?

Thanks in advance

1

1 Answers

0
votes

Try creating your constraints in loadView or some initialization code. Then change your code to this: Try this:

- (void)setAllLandscapeConstraints
{
   self.passwordViewHeight.constant = 34;
}

- (void)setAllPortraitConstraints
{
   self.passwordViewHeight.constant = 40;
}