繁体   English   中英

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

[英]Debug constraints when setting view programmatically

我有一组复杂的标签和按钮。 我主要使用视觉格式语言以编程方式设置所有内容。 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个标签。 它们之间的间距相等,宽度相同。 设备方向始终始终是横向的。

该代码执行了我想要的操作,并且所有内容都显示为OK。 但是我收到一个控制台警告,告诉我系统不能同时满足约束:

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

事实是,我没有在代码的其他任何地方设置这些标签的约束,并且此调试器警告也不能引起太多解释。

警告说您有一个NSAutoresizingMaskLayoutConstraint(它来自自动转换为约束的自动调整大小)。 也许您不仅要在标签上,还要在vistaGris上都设置translatesAutoresizingMaskIntoConstraints=NO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM