为了账号安全,请及时绑定邮箱和手机立即绑定

iOS:autolayout,多个label如何设置居中显示?

iOS:autolayout,多个label如何设置居中显示?

慕后森 2019-04-16 20:27:52
刚学autolayout,关于这个问题,一直没搞清楚!疑惑一:多个view(label)如何设置居中显示?c[backviewaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[lab_before][lab_middle][lab_after]|"options:0metrics:nilviews:NSDictionaryOfVariableBindings(lab_before,lab_middle,lab_after)]];枚举类型参数中:ctypedefNS_OPTIONS(NSUInteger,NSLayoutFormatOptions){NSLayoutFormatAlignAllLeft=(1
查看完整描述

2 回答

?
MM们

TA贡献1886条经验 获得超2个赞

刚刚算是勉强自己解决了:方法比较笨,即用一个backview来包含3个label,然后计算3个label的宽度并赋值给外层的backview,最后把backview居中显示,就完成了3个label的居中显示!如果有更好的方法,欢迎补充,讨论!
层次结构是:
c-view
-backview
-label1
-label2
-label3
c//设置水平布局
[backviewaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[lab_before]-[lab_middle]-[lab_after]|"options:0metrics:nilviews:NSDictionaryOfVariableBindings(lab_before,lab_middle,lab_after)]];
c//设置backview宽度
[viewaddConstraint:[NSLayoutConstraintconstraintWithItem:backviewattribute:NSLayoutAttributeCenterXrelatedBy:NSLayoutRelationEqualtoItem:dynaContentViewattribute:NSLayoutAttributeCenterXmultiplier:1.0constant:before_width+middle_width+after_width]];
//设置backview居中显示
[viewaddConstraint:[NSLayoutConstraintconstraintWithItem:backviewattribute:NSLayoutAttributeCenterXrelatedBy:NSLayoutRelationEqualtoItem:dynaContentViewattribute:NSLayoutAttributeCenterXmultiplier:1.0constant:0.0]];
//设置backview的高度
[viewaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:[backview(==34)]-0-|"options:0metrics:nilviews:NSDictionaryOfVariableBindings(backview)]];
问题来了:
有没有方法不计算每个label的宽度并赋值给superview的宽度,而是自动填充superview!
PS:我在测试的时候,发现不设置backview的宽度,就是整个屏幕的宽度!
2015/03/01更新:
c@interfaceCustomView1:UIView
@property(nonatomic,strong)UILabel*label1;
@property(nonatomic,strong)UILabel*label2;
@end
c@implementationCustomView1
-(id)init{
self=[superinit];
if(self){
_label1=[UILabelnew];
_label1.text=@"AAAAAA121212121";
_label1.textColor=[UIColorblackColor];
_label1.translatesAutoresizingMaskIntoConstraints=NO;
[selfaddSubview:_label1];
_label2=[UILabelnew];
_label2.text=@"BBBBBB";
_label2.textColor=[UIColororangeColor];
_label2.translatesAutoresizingMaskIntoConstraints=NO;
[selfaddSubview:_label2];
}
returnself;
}
-(void)updateConstraints{
NSDictionary*views=NSDictionaryOfVariableBindings(_label1,_label2);
//label1的宽高
[selfaddConstraint:[NSLayoutConstraintconstraintWithItem:_label1attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:1.0constant:[_label1intrinsicContentSize].width]];
[selfaddConstraint:[NSLayoutConstraintconstraintWithItem:_label1attribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:1.0constant:[_label1intrinsicContentSize].height]];
//label2的宽高
[selfaddConstraint:[NSLayoutConstraintconstraintWithItem:_label2attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:1.0constant:[_label2intrinsicContentSize].width]];
[selfaddConstraint:[NSLayoutConstraintconstraintWithItem:_label2attribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:1.0constant:[_label2intrinsicContentSize].height]];
[selfaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[_label1][_label2]|"options:NSLayoutFormatAlignAllBottommetrics:nilviews:views]];
[selfaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:[_label1]|"options:0metrics:nilviews:views]];
[superupdateConstraints];
}
-(CGSize)intrinsicContentSize{
CGSizel1size=[_label1intrinsicContentSize];
CGSizel2size=[_label2intrinsicContentSize];
returnCGSizeMake(l1size.width+l2size.width,l1size.height);
}
@end
                            
查看完整回答
反对 回复 2019-04-16
?
慕村9548890

TA贡献1884条经验 获得超4个赞

Q:有没有方法不计算每个label的宽度并赋值给superview的宽度,而是自动填充superview?
A:重写superview的updateConstraints和intrinsicContentSize方法
                            
查看完整回答
反对 回复 2019-04-16
  • 2 回答
  • 0 关注
  • 368 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信