更新:
我想像这样创建UI:
如何创建这个UI所有程序
创建一个标签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)]];
我也可以设置垂直约束与视图,但我无法从一个标签到另一个标签设置约束.
解决方法 要创建高度和宽度约束的标签,这里是约束…不要忘了添加标签以查看与addSubvIEw方法UILabel *Label = [[UILabel alloc] init];[Label setTranslatesautoresizingMaskIntoConstraints:NO]; [self.vIEw addSubvIEw:Label];// WIDth constraint[Label addConstraint:[NSLayoutConstraint constraintWithItem:Label attribute:NSLayoutAttributeWIDth relatedBy:NSLayoutRelationEqual toItem:nil attribute: NSLayoutAttributeNotAnAttribute multiplIEr:1 constant:200]];// Height constraint[Label addConstraint:[NSLayoutConstraint constraintWithItem:Label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute: NSLayoutAttributeNotAnAttribute multiplIEr:1 constant:21]];
和斯威夫特
Label.setTranslatesautoresizingMaskIntoConstraints(false) self.vIEw.addSubvIEw(Label) Label.addConstraint(NSLayoutConstraint(item: Label,attribute: .Height,relatedBy: .Equal,toItem: nil,attribute: .NotAnAttribute,multiplIEr: 1,constant: 21)) Label.addConstraint(NSLayoutConstraint(item: Label,attribute: .WIDth,constant: 200))
检查这个link更多的细节
UPDATE
当你更新你的问题,这是我更新的答案…
UILabel *Label1 = [[UILabel alloc] init];[Label1 setTranslatesautoresizingMaskIntoConstraints:NO];UILabel *Label2 = [[UILabel alloc] init];[Label2 setTranslatesautoresizingMaskIntoConstraints:NO];Label1.text = @"Label1";Label1.backgroundcolor = [UIcolor bluecolor];Label2.text = @"Label2";Label2.backgroundcolor = [UIcolor redcolor];[self.vIEw addSubvIEw:Label1];[self.vIEw addSubvIEw:Label2];// WIDth constraint[Label1 addConstraint:[NSLayoutConstraint constraintWithItem:Label1 attribute:NSLayoutAttributeWIDth relatedBy:NSLayoutRelationEqual toItem:nil attribute: NSLayoutAttributeNotAnAttribute multiplIEr:1 constant:280]];// Height constraint[Label1 addConstraint:[NSLayoutConstraint constraintWithItem:Label1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute: NSLayoutAttributeNotAnAttribute multiplIEr:1 constant:21]];// CenterX constraint[self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:self.vIEw attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:Label1 attribute: NSLayoutAttributeCenterX multiplIEr:1 constant:0]];// top constraint[self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:Label1 attribute:NSLayoutAttributetop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuIDe attribute: NSLayoutAttributeBottom multiplIEr:1 constant:40]];// label2[self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:Label1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:Label2 attribute: NSLayoutAttributeLeading multiplIEr:1 constant:0]];// label2.Height = label1.Height[self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:Label1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:Label2 attribute: NSLayoutAttributeHeight multiplIEr:1 constant:0]];// label2.wIDth = label1.wIDth[self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:Label1 attribute:NSLayoutAttributeWIDth relatedBy:NSLayoutRelationEqual toItem:Label2 attribute: NSLayoutAttributeWIDth multiplIEr:1 constant:0]];// label2.top[self.vIEw addConstraint:[NSLayoutConstraint constraintWithItem:Label2 attribute:NSLayoutAttributetop relatedBy:NSLayoutRelationEqual toItem:Label1 attribute: NSLayoutAttributeBottom multiplIEr:1 constant:34]];
结果屏幕
总结以上是内存溢出为你收集整理的ios – 如何设置UILabel只有宽度和高度和约束程序全部内容,希望文章能够帮你解决ios – 如何设置UILabel只有宽度和高度和约束程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)