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

刚学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<NSLayoutFormatAlignAllRight=(1<NSLayoutFormatAlignAllTop=(1<NSLayoutFormatAlignAllBottom=(1<NSLayoutFormatAlignAllLeading=(1<NSLayoutFormatAlignAllTrailing=(1<NSLayoutFormatAlignAllCenterX=(1<NSLayoutFormatAlignAllCenterY=(1<NSLayoutFormatAlignAllBaseline=(1<NSLayoutFormatAlignAllLastBaseline=NSLayoutFormatAlignAllBaseline,
NSLayoutFormatAlignAllFirstBaselineNS_ENUM_AVAILABLE_IOS(8_0)=(1<
NSLayoutFormatAlignmentMask=0xFFFF,
疑惑2:
参数中的NSLayoutFormatAlignAllCenterX,该如何使用?每次使用都回报错!
附上报错信息:
cNSInvalidArgumentException|Unabletoparseconstraintformat:\nOptionsmaskrequiredviewstobealignedonahorizontaledge,whichisnotallowedforlayoutthatisalsohorizontal.\nH:|[lab_before][lab_middle][lab_after]|\n^|(\n\"4libc++abi.dylib0x0000000192c65bb4+16\",\n\"5libc++abi.dylib0x0000000192c65478+0\",\n\"6libobjc.A.dylib0x0000000193478204+0\",\n\"7Foundation0x0000000183ce11cc+0\",\n\"8Foundation0x0000000183b6bf44+1296\"\n)|iPhoneOS|8.1|1.0.0|iPhone7,1";
}]
希望,各位大神指教!
潇潇雨雨
浏览 506回答 2
2回答

弑天下

刚刚算是勉强自己解决了:方法比较笨,即用一个backview来包含3个label,然后计算3个label的宽度并赋值给外层的backview,最后把backview居中显示,就完成了3个label的居中显示!如果有更好的方法,欢迎补充,讨论!层次结构是:c-view-backview-label1-label2-label3c//设置水平布局[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;@endc@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

狐的传说

Q:有没有方法不计算每个label的宽度并赋值给superview的宽度,而是自动填充superview?A:重写superview的updateConstraints和intrinsicContentSize方法
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript