我想以编程的方式创建一个具有高度和宽度的UILabel,然后我想以编程的方式为它添加约束,以定位UILabel。
更新:
我想创建这样的UI:

How to create this UI All programatically
创建一个标签label1的代码类似地,我又创建了两个标签label2和label3
UILabel *label1 = [[UILabel alloc]init];
label1.font = TitleFont;
label1.numberOfLines=0;
label1.text= @"Descriptions";
label1.lineBreakMode=NSLineBreakByWordWrapping;
[label1 sizeToFit];
label1.backgroundColor=[UIColor blueColor];
label1.textColor=[UIColor blackColor];
label1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:label1];现在我可以用下面的代码添加水平约束了
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label1]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(label1)]];我也可以用视图设置垂直约束,但我不能用从一个标签到另一个标签的约束。
https://stackoverflow.com/questions/31428795
复制相似问题