UIView animation layoutIfneeded not working

a 夏天 提交于 2020-01-04 13:48:16

问题


I am using this message to animate a customer view to the lower edge of the device screen. I've connected the layout constraint and set initial constant as -60. When the value is set to 0, I expect the view to animate to top. But unfortunately the expected functionality is not working. Even the method is called, the view is hoped up when interface orientation changes. I am using Xcode 6.4 and iOS 8 as base sdks. Any help will be appreciated.

if (alertVisible)
{
     self.alertViewVerticalConstraint.constant = 0;
    [UIView animateWithDuration:0.25 animations: ^{
        [self.view bringSubviewToFront:self.alertView];
        self.alertView.alpha = 0.5;

        [self.alertView layoutIfNeeded];
    }];
}
else
{
     self.alertViewVerticalConstraint.constant = -60;
    [UIView animateWithDuration:0.25 animations: ^{
        self.alertView.alpha = 0.0;

        [self.alertView layoutIfNeeded];
    }];
}

回答1:


Whenever you want to animate the constraint changes, you have to defines the line within the animation block.

So your code will become something like this,

if (alertVisible)
{
[UIView animateWithDuration:0.25 animations: ^{
    self.alertViewVerticalConstraint.constant = 0;

    [self.view bringSubviewToFront:self.alertView];
    self.alertView.alpha = 0.5;

    [self.view layoutIfNeeded];
 }];
}
else
{
   [UIView animateWithDuration:0.25 animations: ^{
       self.alertViewVerticalConstraint.constant = -60;

       self.alertView.alpha = 0.0;

      [self.view layoutIfNeeded];
}];
}


来源:https://stackoverflow.com/questions/35403792/uiview-animation-layoutifneeded-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!