【问题标题】:Autolayout not animating in iOS 10 but working fine in iOS 9自动布局在 iOS 10 中没有动画,但在 iOS 9 中运行良好
【发布时间】:2017-02-04 17:26:11
【问题描述】:
- (IBAction)btnA:(id)sender {
    if(self.height.constant == 300) {
         self.height.constant = 50;
    } else {
         self.height.constant = 300;
    }

    [self.subView1 setNeedsUpdateConstraints];

    [UIView animateWithDuration:1.0 animations:^{
        [self.subView1 layoutIfNeeded];
    }];
}

我正在使用此代码,但在 iOS10 上它没有设置动画,它只是跳跃增加和减少 mySubview1 的高度。为什么?

相同的代码在iOS9 中运行良好

【问题讨论】:

  • 您可以尝试删除此行并查看它是否有效。 [self.subView1 setNeedsUpdateConstraints];

标签: ios objective-c uiview autolayout ios-autolayout


【解决方案1】:

对于 iOS10,它是视图的超级视图,您必须强制布局。

//Force apply all constraints prior to the change in constant.
[self.view.superview layoutIfNeeded];

//Update constant
self.height.constant = 0;

[UIView animateWithDuration:1.0 animations:^{
    [self.view.superview layoutIfNeeded];
} 

我认为这种行为是由 Xcode8 - iOS10 布局的变化引起的

参考资料:

https://stackoverflow.com/a/39501882

https://stackoverflow.com/a/39548367/1045672

【讨论】:

  • iOS 7 中也存在这个问题。看看这个stackoverflow.com/questions/23028701/…
  • 如果我的视图是 UITableViewCell 的子视图,那我该怎么办?我使用 1. [cell.subView.superView layoutIfNeeded]、2.[cell layoutIfNeeded] 还是 3. [cell.superview layoutIfNeed]?我会用哪个?我正在使用相同的代码,但不适用于 iOS 10.1。请帮我。谢谢
  • @JekilPatel 你试过什么?我是否正确,您想在 uitableviewcell 中为子视图设置动画?试试 [cell.contentview layoutifneeded]
  • @Teffi 是的,你是对的。我正在尝试为 UITableViewCell 中的 subView 设置动画。谢谢回复。让我试试你提供的那个,让你知道。非常感谢
  • @Teffi 我试图用 [cell.contentview layoutifneeded] 将 tableview 动画化为 UITableViewCell 内的子视图,不幸的是它不起作用。我正在做与你回答的相同的代码。
猜你喜欢
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 2023-04-04
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多