自动布局约束更改不会设置动画

Ban*_*tor 9 uiviewanimation ios autolayout ios7 ios10

我正在学习教程中带动画的自动布局

http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was

事情很完美.

当我尝试在我的应用程序中使用此概念时,尝试从下到上设置屏幕(UIView)的动画,当设置屏幕只是一个空的UIView时它很有用,

但是如果我将UILabel作为子视图添加到此设置屏幕,动画就会消失.在从设置屏幕中删除此UILabel时,动画可见.

这是我连接的插座

__weak IBOutlet UIView *settingsView;
__weak IBOutlet NSLayoutConstraint *settingsBottomConstraint;
__weak IBOutlet NSLayoutConstraint *settingsViewHeightConstraint;
Run Code Online (Sandbox Code Playgroud)

View确实加载了设置方法(setupViews)

-(void)setupViews
{
    settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
    [settingsView setNeedsUpdateConstraints];
    [settingsView layoutIfNeeded];
    isSettingsHidden = YES;
}
Run Code Online (Sandbox Code Playgroud)

隐藏/取消隐藏方法

- (IBAction)showSettingsScreen:(id)sender {

    if (isSettingsHidden) {

        settingsBottomConstraint.constant = 0;
        [settingsView setNeedsUpdateConstraints];
        [UIView animateWithDuration:.3 animations:^{
            [settingsView layoutIfNeeded];
        }];
    }
    else{

        settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
        [settingsView setNeedsUpdateConstraints];
        [UIView animateWithDuration:0.3 animations:^{
            [settingsView layoutIfNeeded];
        }];

    }
    isSettingsHidden = !isSettingsHidden;
}
Run Code Online (Sandbox Code Playgroud)

我的问题似乎与UIView Auto Layout Animation的问题类似

Ban*_*tor 37

我找到了答案.

代替,

[settingsView layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)

这条线让它像魅力,

[self.view layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)

我想我们需要在父视图上执行layoutIfNeeded方法,而不仅仅是我们试图设置动画的视图.

更新: 正如codyko的评论中所指出的,这是iOS 7,iOS 10所必需的.对于iOS 8,此问题不存在.

  • 请注意,[settingsView layoutIfNeeded]适用于iOS8,但不适用于iOS7.在iOS7中,您需要调用[self.view layoutIfNeeded],但如果您只在iOS8中工作,则无需更新整个视图的约束. (4认同)