Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding priority to layout constraints

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.

I can add the centerX constraint but I can't seem to give it a priority so it remains 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.

like image 509
Fogmeister Avatar asked Mar 12 '13 15:03

Fogmeister


People also ask

What is priority in constraints?

You can see there's a field named "Priority", this is the priority of a constraint. Constant priority is a number to determine how important is that constraint. The number can range from 1 to 1000, the higher the number goes, the more important a constraint is.

How do I change the priority of a constraint in Swift?

Just click on these constraints in the Attribute Inspector and you will see the option to set the constraint priority. The default is 1000, there are some optional values, Apple recommends us to use 250, 750 and 1000, but in fact, you can choose any number from 0 to 1000.

What are auto layout constraints?

Constraints allow you to control how objects respond to changing frames, while auto layout allows you to control how frames respond to changing objects. In this article, I'll explain how they work and when to use each.

How do you add constraints to a storyboard?

Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.


1 Answers

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];
like image 105
BJ Homer Avatar answered Sep 21 '22 09:09

BJ Homer