Search code examples
iosobjective-cautolayout

Slide In A UIView by updating constraints


I am trying to animate a UIView by updating its constraints. I want to initially place the view at the bottom of the screen (off screen) and then set the top constraint to 0 so that the animation shows the view moving upwards.

I have tried the following with no luck at all, the animation is incorrect.

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
//CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

self.viewSearchWrapper.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:self.viewSearchWrapper];

NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:self.viewSearchWrapper
                                   attribute:NSLayoutAttributeTrailing
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.view
                                   attribute:NSLayoutAttributeTrailing
                                   multiplier:1.0f
                                   constant:0.f];

// Leading

NSLayoutConstraint *leading = [NSLayoutConstraint
                                   constraintWithItem:self.viewSearchWrapper
                                   attribute:NSLayoutAttributeLeading
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.view
                                   attribute:NSLayoutAttributeLeading
                                   multiplier:1.0f
                                   constant:0.f];

// Bottom
NSLayoutConstraint *bottom = [NSLayoutConstraint
                                  constraintWithItem:self.viewSearchWrapper
                                  attribute:NSLayoutAttributeBottom
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.view
                                  attribute:NSLayoutAttributeBottom
                                  multiplier:1.0f
                                  constant:0.f];

// Bottom
NSLayoutConstraint *top = [NSLayoutConstraint
                              constraintWithItem:self.viewSearchWrapper
                              attribute:NSLayoutAttributeTop
                              relatedBy:NSLayoutRelationEqual
                              toItem:self.view
                              attribute:NSLayoutAttributeTop
                              multiplier:1.0f
                              constant:screenHeight];

[self.view addConstraint:trailing];
[self.view addConstraint:bottom];
[self.view addConstraint:leading];
[self.view addConstraint:top];                        

[self.viewSearchWrapper setNeedsUpdateConstraints];

[top setConstant:0.f];

[UIView animateWithDuration:1 animations:^{
    [self.view layoutIfNeeded];
}];

Solution

  • Add layoutIfNeeded to first set the initial constraint to viewSearchWrapper.

    // .... keep the code assigning the constraints as it is.
    
    [self.view layoutIfNeeded];  // This will set the initial frame of viewSearchWrapper
    [self.viewSearchWrapper setNeedsUpdateConstraints];
    [top setConstant:0.f];
    [UIView animateWithDuration:1 animations:^{
       [self.view layoutIfNeeded];
    }];