首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >3个标签宽度相等的自动布局

3个标签宽度相等的自动布局
EN

Stack Overflow用户
提问于 2015-06-01 15:17:40
回答 1查看 62关注 0票数 0

我需要像这样显示3个标签:"|-15-Label1-0-Label2-0- label3 -15|".The label1,label2,label3应该有相等的widths.How我可以使用autolayout和coding.Any建议来获得这个吗?

我已经试过了,但是在我的头像上看不到任何东西。

代码语言:javascript
复制
NSDictionary *views2 = NSDictionaryOfVariableBindings(label1, label2, label3);

[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-leftPadding-[label1]-0-[label2]-0-[label3]-rightPadding-|" options:0 metrics:@{@"leftPadding":[NSNumber numberWithInt:15],@"rightPadding":[NSNumber numberWithInt:15]} views:views2]];

[headerView addConstraint:[NSLayoutConstraint constraintWithItem:label1
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:label2
                                                       attribute:NSLayoutAttributeWidth
                                                      multiplier:1
                                                        constant:0]];

[headerView addConstraint:[NSLayoutConstraint constraintWithItem:label1
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:label3
                                                       attribute:NSLayoutAttributeWidth
                                                      multiplier:1
                                                        constant:0]];

[headerView addConstraint:[NSLayoutConstraint constraintWithItem:label2
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:label3
                                                       attribute:NSLayoutAttributeWidth
                                                      multiplier:1
                                                        constant:0]];


[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label1]|" options:0 metrics:nil views:views2]];
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label2]|" options:0 metrics:nil views:views2]];
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label3]|" options:0 metrics:nil views:views2]];
EN

回答 1

Stack Overflow用户

发布于 2015-06-01 15:31:03

如果您希望某些视图具有相同的宽度,而不考虑它们之间的距离。您应该使用间隔视图的概念。有关Spacer视图的更多信息,请参阅apple文档。我认为是最好的解决方案。使用间隔时,需要将约束应用于间隔视图,这将使标签的宽度变得相等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30567738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档