animation时无法同时满足约束条件

我有六个用户约束设置与IB看起来像这样:

H:|-(593)-[UIView(411)]-(20)-| V:|-(20)-[UIView(288)]-(396)-| 

我通过更改约束然后调用layoutIfNeeded来增长和缩小视图。 例如,为了发展我的观点,我会做:

 H:|-(20)-[UIView(984)]-(20)-| V:|-(20)-[UIView(663)]-(20)-| 

然后打电话

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

这种技术会增长和缩小我的观点,它看起来不错,但我给了一个相当混乱的警告:

 Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSLayoutConstraint:0x148d5af0 H:[UIView:0x148d4e50(411)]>", "<NSLayoutConstraint:0x148cc940 H:[UITableView:0xace7600(319)]>", "<NSLayoutConstraint:0x148ce040 H:|-(NSSpace(20))-[UITableView:0xacd4e00] (Names: '|':UIView:0x148cddd0 )>", "<NSLayoutConstraint:0x148cdf00 H:[UITableView:0xace7600]-(NSSpace(20))-| (Names: '|':UIView:0x148cddd0 )>", "<NSLayoutConstraint:0x148cdea0 H:[UITableView:0xacd4e00]-(NSSpace(8))-[UITableView:0xace7600]>", "<NSLayoutConstraint:0x148d4c10 UIView:0x148cddd0.trailing == UIView:0x148cdd40.trailing>", "<NSLayoutConstraint:0x148d4b90 H:|-(0)-[UIView:0x148cddd0] (Names: '|':UIView:0x148cdd40 )>", "<NSLayoutConstraint:0x148d6020 H:|-(320)-[UIView:0x148cdd40] (Names: '|':UIView:0x148cd330 )>", "<NSLayoutConstraint:0x148d5fa0 UIView:0x148cdd40.trailing == UIView:0x148cd330.trailing>", "<NSLayoutConstraint:0x148d5f60 H:[UIView:0x148d4e50]-(NSSpace(20))-| (Names: '|':UIView:0x148cd330 )>", "<NSLayoutConstraint:0x148d5ee0 H:|-(20)-[UIView:0x148d4e50] (Names: '|':UIView:0x148cd330 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x148cc940 H:[UITableView:0xace7600(319)]> 

现在所有这些约束都是由IB产生的。 我已经双^(三重!)检查了这一点。 我用笔和纸把这些限制放在一起,得到了这个:

 UIView_A H:[-(20)-[UIView_E]-(20)-] and H:[-(320)-(UIView_B) UIView_B H:[-(0)-[UIView_D] UIView_C H:[UIView_C(411)] UIView_D H:[-(20)-[UITableView_F]-[UITableView_G(319)]-(20)-] 

我不明白这些约束如何不能得到满足。 他们看起来很好。 我不会改变它们,它们是由IB生成的。 IB产生的约束是否自动满足?

或者,至less,有没有办法阻止警告? 它的performance完美,我不需要看到这是一个违反约束,似乎没有任何反应。

这个约束:

H:[UITableView的:0xace7600(319)]>”

似乎是系统解决scheme的障碍。

你可以删除它吗?

所以,事实certificate,我改变限制的顺序很重要。

为了发展观点,我会的

  1. 增加宽度:H:| – (593) – [UIView( 984 )] – (20) – |
  2. 减less领导空间:H:| – ( 20 ) – [UIView(984)] – (20) – |

这不会产生警告。 但是,如果我按照相反的顺序执行此操作,则会发出警告:

 Unable to simultaneously satisfy constraints. Probably at least one of the constraints ... ...Will attempt to recover by breaking constraint <NSLayoutConstraint:0x1567f650 H:[UITableView:0x119e6200(319)]> 

缩小视图时,我复制了增加宽度的代码(使用相同的顺序),只是更改了值。 这给了我在我原来的问题上发布的警告。 当我缩小订单时,警告消失了。


这是为什么? 我不知道。 我会更新,因为我发现更多。

我得到了同样的信息,我终于明白了为什么会发生,
我的解决scheme是:在animation的任何时刻,不要让任何对象翻转。

换句话说,限制是应该在任何对象的外面,但有时限制在animation里面,而不是像我们的期待。

换句话说,不要让顶边由于约束animation而侵入底边。

例如,

  top constraint: topA = initially 100 [Box A] bottom constraint: botA = initially 150 

现在,如果你像下面设置和animation,

  topA = 300 botA = 25 

那么应该发生错误,为什么:线程侵入底部边缘之前底部边缘下降。 所以,你宁愿改变订单,

  botA = 25 topA = 300 

那么错误将会消失,因为底部约束将保持质量的高度,而下一个顶部约束会缩小对象的高度而不是侵入底部边界。

* Point:即使在animation中,让对象的宽度和高度大于0 CONTINUALLY也不会被约束改变中断。

我希望我帮助你。