动画时无法同时满足约束

时间:2013-03-02 00:23:35

标签: ios cocoa-touch uicollectionview uiviewanimation autolayout

我在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生成的约束不是自动满足吗?

或者,至少,有没有办法停止警告?它表现得很完美,而且我不需要看到它打破了一个似乎无论如何都没有做任何事情的约束。

3 个答案:

答案 0 :(得分:4)

此约束:

  

H:[UITableView的:0xace7600(319)]&gt;“中

似乎是系统解决方案的障碍。

你可以删除吗?

答案 1 :(得分:3)

事实证明,我改变约束的顺序很重要。

为了增加视野,我会

  1. 增加宽度:H:| - (593) - [UIView( 984 )] - (20) - |
  2. 减少领先空间:H:| - ( 20 ) - [UIView(984)] - (20) - |
  3. 这不会产生任何警告。但是,如果我按相反的顺序执行此操作,我会收到警告:

    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)]>
    

    缩小视图时,我复制了增加宽度的代码(具有相同的顺序)并且只更改了值。这给了我在原始问题中发布的警告。当我改变订单收缩时,警告消失了。


    为什么会这样?我不知道。我会在发现更多时更新。

答案 2 :(得分:0)

我收到同样的信息,我终于知道了为什么会发生这种情况 我的解决方案是:在动画的任何时刻都不要让任何对象FLIP OVER。

换句话说,约束应该超出任何对象, 但有时在动画期间会出现约束,而不是像我们的预期。

换句话说,不要让顶部边距因约束动画而侵入底部边距。

例如,

     top constraint: topA = initially 100

                    [Box A]

     bottom constraint: botA = initially 150

现在,如果你设置如下并动画,

      topA = 300
      botA = 25  

那么应该发生错误, 原因:线程在底部边距下降之前侵入底部边距。 所以, 你宁愿改变ORDER,

      botA = 25
      topA = 300

然后错误将消失,因为底部约束将保留质量的高度,而下一个顶部约束会缩小对象的高度而不会侵入底部边距。

*点: 即使在动画过程中,对象的宽度和高度也大于0连续 不受约束变化的干扰。

我希望我帮助过你。