【问题标题】:Auto Layout constraint change does not animate自动布局约束更改没有动画
【发布时间】:2014-05-26 13:26:23
【问题描述】:

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

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;

View 确实加载了设置方法(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

【问题讨论】:

  • 你的 UILabel 有什么限制?
  • @Iftekhar,我已经尝试了这两个选项。 1. 只需将标签放到 UIView(即没有约束)和 2. 将宽度、高度、顶部空间的约束添加到 superview 并导致 superview。没有工作。

标签: ios ios7 autolayout uiviewanimation ios10


【解决方案1】:

我找到了答案。

而不是,

[settingsView layoutIfNeeded];

这条线让它像魅力一样发挥作用,

[self.view layoutIfNeeded];

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

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

【讨论】:

  • 请注意,[settingsView layoutIfNeeded] 适用于 iOS8,但不适用于 iOS7。在 iOS7 中你需要调用 [self.view layoutIfNeeded],但是如果你只在 iOS8 中工作,你不需要为整个视图更新约束。
猜你喜欢
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多