3

I am trying to animate a UIWebView in from the bottom on top of my viewController, and I am using masonry (https://github.com/Masonry/Masonry).

I initially create my webview with the size of 0 - (x,y,height and width), and I then try to animate it so that the webview animates "on top" of the viewcontroller. The webview is shown, but it doesn't animate in place - it just appears immediately. Can anyone who has experience guide me in the right direction?

This is my button action

-(void)didPressBtn{
    self.infoView = [UIWebView new];
    self.infoView.scalesPageToFit = YES;
    self.infoView.backgroundColor = [UIColor whiteColor];
    self.infoView.delegate = self;
    [self.scrollView addSubview:self.infoView];
    [self.infoView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];


    [self.scrollView layoutIfNeeded];
    //FIXME: Hmmm it doesn't really animate!?
    [UIView animateWithDuration:1 animations:^{
        [self.scrollView setContentOffset:CGPointMake(0, 0)];
        [self.infoView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.scrollView);
        }];

        [self.scrollView layoutIfNeeded];

    } completion:^(BOOL finished) {
                [self.infoView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:NSLocalizedString(@"INFORMATION_URL", )] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15]];
    }];
}

My viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.scrollView = [UIScrollView new];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    UIView *contentView = [UIView new];
    [self.scrollView addSubview:contentView];

    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];

//Then adding buttons and such...
}
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
jacob
  • 541
  • 1
  • 9
  • 19

1 Answers1

17

One quick observation regarding didPressButton. You are using mas_makeConstraints to set the edges equal to @0 and them immediately afterwards using mas_makeConstraints again to change them so that the scrollview is filled. This adds contradictory constraints on top of the first set.

Instead, you can use mas_remakeConstraints to replace the existing constraints on that view.

As for the animation, the pattern I use is to first update the constraints (not inside an animation block) and then animate a layout:

[UIView animateWithDuration:1
        animations:^{
            [theParentView layoutIfNeeded];
        }];

See also How do I animate constraint changes?

Community
  • 1
  • 1
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
  • 3
    thanks! I always forget that `layoutIfNeeded` should be called on the **parentView**, not on the animating view itself – boweidmann Oct 15 '15 at 13:43
  • 2
    I had this problem again, googled it, came here, realized I was calling `layoutIfNeeded` on a subview, wanted to upvote this answer but found I already upvoted it. Then I wanted to at least upvote the comment but then realized that was *my* comment from 3 years ago. Darn, humans are oblivious.... – boweidmann Oct 06 '18 at 15:06