添加优先布局限制 [英] Adding priority to layout constraints

查看:113
本文介绍了添加优先布局限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签按钮上海华是这样的。

I have a label and button in a superView like this.

|--------------[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.

因此​​,如果按钮是大它看起来像...

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.

我可以添加的centerX 约束,但是我似乎无法给它一个优先级,以便它仍然要求

I can add the centerX constraint but I can't seem to give it a priority so it remains Required.

如何创建这种情况呢?我做在code所有的自动布局。

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:

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

这篇关于添加优先布局限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆