以编程方式设置视图时调试约束

时间:2014-04-19 08:11:28

标签: ios autolayout nslayoutconstraint

我有一套复杂的标签和按钮。我主要使用可视化格式语言设置所有内容。 UILabel使用以下方法创建:

-(UILabel*)createLabelWithPlaceHolder:(NSString*)placeHolder{

    UILabel *provisionalLabel=[[UILabel alloc] init];

    provisionalLabel =[[UILabel alloc] init];
    provisionalLabel.translatesAutoresizingMaskIntoConstraints=NO;
    provisionalLabel.text=placeHolder;
    provisionalLabel.font=[UIFont systemFontOfSize:12.0f];
    provisionalLabel.backgroundColor=[UIColor greenColor];
    provisionalLabel.textAlignment=NSTextAlignmentCenter;
    [provisionalLabel setContentHuggingPriority:200 forAxis:UILayoutConstraintAxisHorizontal];
    [provisionalLabel setContentCompressionResistancePriority:900 forAxis:UILayoutConstraintAxisHorizontal];

    return provisionalLabel;
}

并根据以下VLF代码显示标签:

gap=(self.vistaGris.bounds.size.width*secondRowFactor)/7;
labelWidth=(self.vistaGris.bounds.size.width*(1-secondRowFactor))/6;

gapN=[NSNumber numberWithFloat:gap];
labelWidthN=[NSNumber numberWithFloat:labelWidth];

constraints = [NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-gap-[solution4(width)]-gap-[solution5(width)]-gap-[solution6(width)]-gap-[solution7(width)]-gap-[solution8(width)]-gap-[solution9(width)]-gap-|"
                                                      options:NSLayoutFormatAlignAllBaseline
                                                      metrics:@{@"gap":gapN, @"width":labelWidthN}
                                                        views:viewsDictionary];

[self.vistaGris addConstraints:constraints];

基本上它的作用是在视图的底部显示6个标签。它们之间的空间相等,宽度相同。设备方向始终是横向的。

代码执行我想要的操作,一切都显示正常。但是我得到一个控制台警告告诉我系统不能满足同时约束:

2014-04-19 09:59:48.749 Concentrations[431:60b] 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:0x8d6cd70 Linees:0x8c82d70.width == UIView:0x8c41c40.width>",
    "<NSLayoutConstraint:0x8d84480 H:|-(6.85714)-[UILabel:0x8d65460]   (Names: '|':Linees:0x8c82d70 )>",
    "<NSLayoutConstraint:0x8d844d0 H:[UILabel:0x8d65460(72)]>",
    "<NSLayoutConstraint:0x8d84500 H:[UILabel:0x8d65460]-(6.85714)-[UILabel:0x8d73890]>",
    "<NSLayoutConstraint:0x8d84550 H:[UILabel:0x8d73890(72)]>",
    "<NSLayoutConstraint:0x8d845b0 H:[UILabel:0x8d73890]-(6.85714)-[UILabel:0x8d731b0]>",
    "<NSLayoutConstraint:0x8d845e0 H:[UILabel:0x8d731b0(72)]>",
    "<NSLayoutConstraint:0x8d84640 H:[UILabel:0x8d731b0]-(6.85714)-[UILabel:0x8d734e0]>",
    "<NSLayoutConstraint:0x8d84690 H:[UILabel:0x8d734e0(72)]>",
    "<NSLayoutConstraint:0x8d84700 H:[UILabel:0x8d734e0]-(6.85714)-[UILabel:0x8d741b0]>",
    "<NSLayoutConstraint:0x8d84730 H:[UILabel:0x8d741b0(72)]>",
    "<NSLayoutConstraint:0x8d847d0 H:[UILabel:0x8d741b0]-(6.85714)-[UILabel:0x8d744d0]>",
    "<NSLayoutConstraint:0x8d84800 H:[UILabel:0x8d744d0(72)]>",
    "<NSLayoutConstraint:0x8d84860 H:[UILabel:0x8d744d0]-(6.85714)-|   (Names: '|':Linees:0x8c82d70 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x8c8aef0 h=--& v=--& V:[UIView:0x8c41c40(480)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8d847d0 H:[UILabel:0x8d741b0]-(6.85714)-[UILabel:0x8d744d0]>

问题是我没有在我的代码上的任何其他地方设置这些标签的约束,这个调试器警告也不能解释。

1 个答案:

答案 0 :(得分:0)

警告说你有一个NSAutoresizingMaskLayoutConstraint(它来自autoresize自动转换为约束)。您可能不仅要在标签上设置translatesAutoresizingMaskIntoConstraints=NO,还要在vistaGris上设置{{1}}。