简体   繁体   English

添加布局约束的优先级

[英]Adding priority to layout constraints

I have a label and button in a superView like this. 我在这样的superView有一个labelbutton

|--------------[Label]-----[button]-|

I'd like the label to be centred if possible, then have a min gap to the button and move to the left. 如果可能的话,我希望label centred ,然后button的最小间隙向左移动。

So if the button is big it looks like... 所以,如果按钮很大,它看起来像......

|-[        LABEL!        ]-[button]-|

So, the button stays where it is and at the same size. 因此,按钮保持原样并且尺寸相同。 And there are minimum gaps between the elements. 并且元素之间存在最小间隙。

I can add the centerX constraint but I can't seem to give it a priority so it remains Required . 我可以添加centerX约束,但我似乎无法给它一个优先级,所以它仍然是Required

How can I create this situation? 我该如何创造这种情况? I'm doing all the auto layout in code. 我在代码中进行所有自动布局。

The constraints I currently have are... 我目前的限制是......

[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]];

But I'm not sure how to reduce the priority of the second constraint. 但我不确定如何降低第二个约束的优先级。

You just set the priority property of the constraint, like so: 您只需设置约束的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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM