I have set my constraints programmatically using visual format like this
[[self contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label1]-(==space1@1000)-[label2]-(>=space2@750)-|" options:0 metrics:metricsDictionary views:viewDictionary]];
Since I needed the leading space between [self contentView]
and label1
to be dynamic I created a property and initialized it like this
[self setLeadingConstraint:[NSLayoutConstraint
constraintWithItem:[self label1]
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:[self contentView]
attribute:NSLayoutAttributeLeading
multiplier:1.0f constant:kLeadingSpace]];
I added all the constraints to [self contentView]
and so far is working fine.
This is how it looks when label 1 is a long string
This is how it looks when label 1 is a short string
Now I need the trailing space to be dynamic as the leading space so I tried the same and initialized visual format constraints the variable trailing constraint as follows
[[self contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label1]-(==space1@1000)-[label2]" options:0 metrics:metricsDictionary views:viewDictionary]];
[self setTrailingSpaceConstraint:[NSLayoutConstraint
constraintWithItem:[self label2]
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:[self contentView]
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:kTrailingSpace]];
This time the trailing constraint is not set properly as images show