自动布局约束更改不animation

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

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

事情正在完善。

当我尝试在我的应用程序中使用这个概念时,试图从底部到顶部设置一个设置屏幕(UIView)的animation,当设置屏幕只是一个空的UIView时,

但是,如果我添加一个UILabel作为子视图到这个设置屏幕,animation就消失了。 在从设置屏幕删除这个UILabel时,animation是可见的。

这是我连接的网点

__weak IBOutlet UIView *settingsView; __weak IBOutlet NSLayoutConstraint *settingsBottomConstraint; __weak IBOutlet NSLayoutConstraint *settingsViewHeightConstraint; 

查看没有负载设置方法(setupViews)

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

隐藏/取消隐藏方法

 - (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; } 

我的问题似乎与UIView自动布局animation问题类似

我find了答案。

代替,

 [settingsView layoutIfNeeded]; 

这条线使它像魅力一样工作,

 [self.view layoutIfNeeded]; 

我想我们需要在父视图上执行layoutIfNeeded方法,而不仅仅是我们想要animation的视图。

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