2013-03-07 58 views
5

我已经使用自动版式约束IB之前试过,我了解他们的工作,他们必须明确等等应用NSLayoutConstraints到UIView子类

但是当你有超过一对夫妇的意见更它变得有点复杂并且往往会破裂。

所以,我刚刚读了一篇关于一个有相同问题的人的博客,并使用ASCII代码来创建约束。

我有一个UITableView与自定义的UITableViewCell具有基于内容量的动态大小。自动布局的完美人选。

所以,我尝试了几种方法来做到这一点,现在我已经减少了内容,以便在单元格中只有一个标签。

我想如果标签填充单元格周围有一个边框。

即标准尺寸从单元的每个边缘进入。

我已经做了这样的...

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 
     _label.textColor = [UIColor colorWithWhite:0.53 alpha:1.0]; 
     _label.backgroundColor = [UIColor yellowColor]; 
     [self addSubview:_label]; 

     NSDictionary *views = NSDictionaryOfVariableBindings(_label); 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_label]-|" 
                    options:0 
                    metrics:nil 
                     views:views]]; 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-|" 
                    options:0 
                    metrics:nil 
                     views:views]]; 

     self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
     self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

但在显示单元当标签是在左上角酷似的CGRect我用来初始化它,我得到这个负载的约束错误...

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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>", 
    "<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a4f0 H:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.841 unasys[13082:907] 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:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f7b0 V:|-(NSSpace(20))-[UILabel:0x1f864a00] (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.854 unasys[13082:907] 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:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e40 h=--& v=--& UILabel:0x1f864a00.midX == + 5>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859e80 h=--& v=--& H:[UILabel:0x1f864a00(10)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1ed8e150 h=--& v=--& H:[MyCell:0x1f857740(320)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f86a540 H:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2013-03-07 11:56:14.858 unasys[13082:907] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1eda7260 h=--& v=--& V:[MyCell:0x1f857740(143)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859f00 h=--& v=--& V:[UILabel:0x1f864a00(10)]>", 
    "<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x1f859ec0 h=--& v=--& UILabel:0x1f864a00.midY == + 5>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1f85f870 V:[UILabel:0x1f864a00]-(NSSpace(20))-| (Names: '|':MyCell:0x1f857740)> 

我不知道从哪里开始调试这个。约束是绝对最小的,但仍然完全失败。

有人能指出我如何得到这个约束的正确方向吗?

回答

13

行后

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 

添加

[_label setTranslatesAutoresizingMaskIntoConstraints:NO]; 

默认情况下,autoresizingmask也变成了限制,这与您所设定的限制冲突,使得它ambigious

的docs are here:setTranslatesAutoresizingMaskIntoConstraints

+0

太棒了!非常感谢! – Fogmeister 2013-03-07 12:22:35

+0

作品!我也必须改变添加东西到self.contentView而不是自己,因为它是一个UITableViewCell,然后它的一切工作:D谢谢! – Fogmeister 2013-03-07 13:13:16

+1

基本上,如果您在自动布局错误中看到“NSAutoresizingMaskLayoutConstraint”,并且您不知道它为什么存在,那么您忘记了在某处关闭了'translatesAutoresizingMaskIntoConstraints' – iain 2013-03-07 13:37:14