我在这样的 superView
中有一个 label
和 button
。
|--------------[Label]-----[button]-|
如果可能的话,我希望标签
居中
,然后与按钮
保持最小距离并向左移动。
所以如果按钮很大,它看起来像...
|-[ LABEL! ]-[button]-|
因此,按钮将保持原样且大小不变。元素之间的间隙最小。
我可以添加 centerX
约束,但我似乎无法赋予它优先级,因此它仍然是 Required
。
我怎样才能创造这种情况?我正在代码中进行所有自动布局。
我目前的限制是...
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[_label]-(>=8@1000)-[_button(==45)]-|"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:views]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_label
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
但我不确定如何降低第二个约束的优先级。
最佳答案
您只需设置约束的 priority
属性,如下所示:
NSLayoutConstraint *centeringConstraint =
[NSLayoutConstraint constraintWithItem:_label
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0];
centeringConstraint.priority = 800; // <-- this line
[self addConstraint:centeringConstraint];
关于iphone - 为布局约束添加优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15365286/