添加布局约束的优先级

Fog*_*ter 16 iphone objective-c ios autolayout

我有一个labelbuttonsuperView这样的.

|--------------[Label]-----[button]-|
Run Code Online (Sandbox Code Playgroud)

我想的labelcentred如果可能的话,那么有差距最小的button,并向左移动.

所以,如果按钮很大,它看起来像......

|-[        LABEL!        ]-[button]-|
Run Code Online (Sandbox Code Playgroud)

因此,按钮保持原样并且尺寸相同.并且元素之间存在最小间隙.

我可以添加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]];
Run Code Online (Sandbox Code Playgroud)

但我不确定如何降低第二个约束的优先级.

BJ *_*mer 36

您只需设置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];
Run Code Online (Sandbox Code Playgroud)