刚学autolayout,关于这个问题,一直没搞清楚!
c
[backview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[lab_before][lab_middle][lab_after]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lab_before,lab_middle,lab_after)]];
枚举类型参数中:
c
typedef NS_OPTIONS(NSUInteger, NSLayoutFormatOptions) { NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft), NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight), NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop), NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom), NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading), NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing), NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX), NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY), NSLayoutFormatAlignAllBaseline = (1 << NSLayoutAttributeBaseline), NSLayoutFormatAlignAllLastBaseline = NSLayoutFormatAlignAllBaseline, NSLayoutFormatAlignAllFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0) = (1 << NSLayoutAttributeFirstBaseline), NSLayoutFormatAlignmentMask = 0xFFFF,
参数中的NSLayoutFormatAlignAllCenterX,该如何使用?每次使用都回报错!
c
NSInvalidArgumentException|Unable to parse constraint format: \nOptions mask required views to be aligned on a horizontal edge, which is not allowed for layout that is also horizontal. \nH:|[lab_before][lab_middle][lab_after]| \n ^|(\n \"4 libc++abi.dylib 0x0000000192c65bb4 <redacted> + 16\",\n \"5 libc++abi.dylib 0x0000000192c65478 <redacted> + 0\",\n \"6 libobjc.A.dylib 0x0000000193478204 <redacted> + 0\",\n \"7 Foundation 0x0000000183ce11cc <redacted> + 0\",\n \"8 Foundation 0x0000000183b6bf44 <redacted> + 1296\"\n)|iPhone OS|8.1|1.0.0|iPhone7,1"; }]
希望,各位大神指教!
I just forced myself to solve it: the method is rather stupid, that is, use a backview to contain 3 labels, then calculate the width of the 3 labels and assign them to the outer backview, and finally center the backview to complete the 3 labels. The label is displayed in the center! If there is a better method, please add it and discuss it!
The hierarchy is:
Here comes the question:
Is there a way not to calculate the width of each label and assign it to the width of the superview, but to automatically fill the superview!
PS: When I was testing, I found that if the width of the backview is not set, it will be the width of the entire screen!
Updated on 2015/03/01:
Q: Is there a way to automatically fill the superview without calculating the width of each label and assigning it to the width of the superview?
A: Override superview’s updateConstraints and intrinsicContentSize methods
A containerview contains 3 labels
Do not specify the width for these three labels,
Constrain containerview and left+right margin constraints
Constrain the spacing relationship between left center right labels
Constrain contrainerview to center
I have also been troubled by this problem. If I want to use multiple views in the play, I need to apply a View externally. If I manually set the calculation frame, this problem will not occur. It can be seen that autolaout also has shortcomings.