2

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 Not dynamic trailing space constraint with long string

This is how it looks when label 1 is a short string Not dynamic trailing space constraint with 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

Dynamic trailing space constraint with long string Dynamic trailing space constraint with short string

Black Sheep
  • 1,665
  • 1
  • 22
  • 32
  • 1
    kTrailingSpace needs to be a negative number, if it's not currently. It would be helpful to know what you're trying to accomplish by making the constraints dynamic. You should describe what you want to happen with the 2 labels as their text changes (if that's what you mean by dynamic). – rdelmar Apr 08 '15 at 21:38
  • I tried that but is not working – Black Sheep Apr 08 '15 at 22:25
  • That's not a useful response. What you're trying to do is unclear. If you want some help you need to clarify your question. – rdelmar Apr 08 '15 at 22:45
  • You are adding a trailing constraint 2 times. Once in the first code snippet (which is greater than space2) and once with your trailingConstraint. That could be the cause of it not working. Try removing one of them and see the result – Catalina T. Apr 09 '15 at 07:16
  • Label2 width (text) is fixed, label1 is variable, if label1 is a short string then label2 should be next to label1 (something like left alignment), but if label1 is lengthy should truncate and label2 should fit within the container view with the trailing space. – Black Sheep Apr 09 '15 at 15:24
  • Hi @CatalinaT., the visual format constraints look like this `@"H:[label1]-(==space1@1000)-[label2]"` when I'm creating the trailing space constraint as a variable – Black Sheep Apr 09 '15 at 15:26
  • Would you get the desired effect if you set label2.width to be equal or greater than its desired size, align the text left, and set a constant trailing to 0 points? – Bjørn Ruthberg Jun 20 '16 at 08:21

0 Answers0