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.

  • All RestKit projects fail to build when archiving
  • Drawing Circular Gradient
  • UITableView set to static cells. Is it possible to hide some of the cells programmatically?
  • Detect when WKWebView has finished loading EVERY time
  • Display animated GIF in iOS
  • CellForRowAtIndexPath is not invoked
  • 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.

    Solutions Collect From Internet About “Adding priority to layout constraints”

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