我正在使用教程中的动画学习自动布局
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;
查看了加载设置方法(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;
}
我的问题似乎与此类似 Issue with UIView Auto Layout Animation
答案 0 :(得分:36)
我找到了答案。
而不是,
[settingsView layoutIfNeeded];
这条线让它像魅力一样,
[self.view layoutIfNeeded];
我想我们需要在父视图上执行layoutIfNeeded方法而不仅仅是我们试图设置动画的视图。
<强>更新强> 正如codyko的评论中所指出的, iOS 7,iOS 10 需要这样做。 对于iOS 8,此问题不存在。