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.

  • iOS Gyroscope API
  • How to crop the image using UIBezierPath?
  • How to show the progress of copying a large file in iOS?
  • Carthage: no shared framework schemes for iOS platform (for my own framework)
  • Are the sorting algorithms used by NSArray stable sorts?
  • Rounded rect using UIRectCorner in Swift not working
  • How do I do this in Auto Layout? Preferably in Interface Builder?

    2 Solutions Collect From Internet About “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)?”

    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.

    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.