Doug Smith
Doug Smith

Reputation: 29326

Using Auto Layout, how do I make two labels on the same "line" be dynamic in their widths (adjusting with how wide each one needs to be)?

Say I have two UILabels, one is a URL and one is a timer:

[http://website.com/link/to/specific/story] [9m 42s]

But the "timer" UILabel changes width a lot. Sometimes it is just 12s for example, other times it is 125h 32m 8s. There's a lot of variability. I want the timer UILabel to take up as much width as it needs and the URL label to take up the rest. This means if the timer is longer, shorter URLs, etc.

How do I do this in Auto Layout? Preferably in Interface Builder?

Upvotes: 22

Views: 7592

Answers (2)

hybridcattt
hybridcattt

Reputation: 3041

You should set the compression resistance, URL label must have lower value;

Set the horizontal space between labels to constant (for example zero or default);

Set width for each label and select "less than" or "greater that" relation, these values set limits for shrinking/expanding of the view.

Upvotes: 21

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

This is where compression resistance becomes important. Since the URL label needs to shrink first (in order to accommodate the space for the timer label), the compression resistance of the URL label should be lower than the compression resistance of the timer label (in horizontal direction, of course). You can easily set this attribute in IB.

Upvotes: 12

Related Questions