iOS自动布局无法同时满足约束

时间:2014-05-08 07:38:05

标签: ios nslayoutconstraint nsautolayout

我的代码段如下

    self.sortOptionsViewSmall.hidden = !self.sortOptionsViewSmall.hidden;
    self.sortOptionsView.hidden=YES;
    [UIView animateWithDuration:0.4 animations:^{

        **self.sortViewHeightSmall.constant = (self.sortViewHeightSmall.constant == 0) ? 187 : 0;**
        self.sortViewHeightConstraint.constant = (self.sortViewHeightSmall.constant == 187) ? 187 : 0;
        self.tableViewHeightConstraint.constant = self.sortViewHeightSmall.constant == 0 ? 664 : 477;

        [self.view layoutIfNeeded];
    }];

如果我导航到其他视图并导航回

self.sortViewHeightSmall.constant = (self.sortViewHeightSmall.constant == 0) ? 187 : 0;

给我以下错误

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:0x181643e0 V:[SortOptionsView:0x18162970(187)]>",
"<NSLayoutConstraint:0x18162ac0 V:[SortOptionsView:0x18162010(0)]>",
"<NSLayoutConstraint:0x18155170 V:[SortOptionsView:0x18162970]-(0)-    
[UITableView:0xea98e00]>",
"<NSLayoutConstraint:0x181551a0 V:[SortOptionsView:0x18162010]-(0)-
[UITableView:0xea98e00]>",
"<NSLayoutConstraint:0x18155260 V:[UIView:0x181544e0]-(0)-
[SortOptionsView:0x18162010]>",
"<NSLayoutConstraint:0x18155290 V:[UIView:0x181544e0]-(0)-
[SortOptionsView:0x18162970]>"
)

将尝试通过违反约束来恢复

<NSLayoutConstraint:0x181643e0 V:[SortOptionsView:0x18162970(187)]>

中断objc_exception_throw以在调试器中捕获它。 UIConstraintBasedLayoutDebugging中列出的UIView类别中的方法 <UIKit/UIView.h>也可能有所帮助。这个错误。**

我尝试使用setTranslatesAutoresizingMaskIntoConstraints:NO,但它无效。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

检查您的代码。 在错误堆栈中,我看到两个冲突约束:

"<NSLayoutConstraint:0x181643e0 V:[SortOptionsView:0x18162970(187)]>",
"<NSLayoutConstraint:0x18162ac0 V:[SortOptionsView:0x18162010(0)]>",

SortOptionsView无法理解,它的高度大小是187点还是0? 您可以通过两种方式解决此冲突:

  1. 删除一个constrait;
  2. 为每个冲突限制设置优先级;
  3. 我希望我能把你送上正确的道路。