Adding priority to layout constraints
I have a
button in a
superView like this.
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
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.
- AutoLayout to keep view sizes proportional
- How can I trigger to update all the autolayout constrains?
- Auto Layout: Square Image View with equal width / height
- Change UIImageView size to match image with AutoLayout
- Why does iOS auto layout lead to apparent rounding errors on pre-Retina displays (unit test included)
- if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction - could this mean something here?
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];
- Swift Swipe Navigation Table Views
- Delete an unreferenced image from repository in Xcode
- Swift NSLayoutConstraint Equation Based Constant
- iOS swift NSMutableData has no member appendString
- Is it possible to disable the network in iOS Simulator?
- Replace, move objects in NSMutableArray
- facebook login give me already authorized this app without automatic returning to the app
- Force Touch animation freezes if gently touched
- Issue in Xcode 8.1 Storyboard layouts
- Apple Mach-O Linker Error lxml
- swift 3 error : Argument labels '(_:)' do not match any available overloads
- Calling UIViewController method from app delegate
- iOS Storyboards Should I use them or not?
- Make a line thicker in 3D?
- jQuery Mobile fixed footer is moving when the keyboard appears