gpt4 book ai didi

ios - 当 iOS 上的状态栏高度发生变化时,基于约束的布局不会动画化

转载 作者:技术小花猫 更新时间:2023-10-29 10:53:31 25 4
gpt4 key购买 nike

我有这个 View ,它曾经有 autoresizingMask = UIViewAutoresizingFlexibleHeight

当状态栏对其高度进行动画处理时(例如挂断电话时), View 的高度将进行动画处理并增加。

但是对于自动布局,我用约束替换了这个 autoresizingMask:

UIView *orangeView = [[UIView alloc] initWithFrame:CGRectZero];
orangeView.translatesAutoresizingMaskIntoConstraints = NO;
orangeView.backgroundColor = [UIColor orangeColor];

[self.view addSubview:orangeView];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[orangeView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(orangeView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(40)-[orangeView]-(190)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(orangeView)]];

但是现在,我的布局变化没有状态栏动画,它只是在没有任何动画的情况下发生变化。

现在我知道在使用基于约束的布局时我应该在动画 block 中调用 -layoutIfNeeded。但在这里,我不是创建动画 block 的人!那么有没有办法让变化动起来呢?

这是否意味着我必须在我的代码中找到一个位置,该位置将在我未启动的动画 block 期间执行?当 UIApplicationWillChangeStatusBarFrameNotification 被触发时,我试图在我的 Controller 中设置 [self.view layoutIfNeeded],但它不起作用。

最佳答案

确保在 updateConstraints 方法中添加约束。

这是 docs 的内容说:

Custom views that set up constraints themselves should do so by overriding this method. When your custom view notes that a change has been made to the view that invalidates one of its constraints, it should immediately remove that constraint, and then call setNeedsUpdateConstraints to note that constraints need to be updated.

关于ios - 当 iOS 上的状态栏高度发生变化时,基于约束的布局不会动画化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15522698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com