Reputation: 73031
I am trying to achieve the following design with auto-layout constraints:
The middle label is straightforward (Horizontally Center to view).
The left and right labels are not as straightforward. Simply adding Leading and Trailing Space constraints of 20 will not work for all content. Furthermore, the detail label underneath can be wider.
I thought about give the right and left labels both Leading and Trailing Space constraints of >= 20 and centering the text.
How can I achieve this column layout of 3 labels with dynamic content while:
Upvotes: 2
Views: 2495
Reputation: 6729
I solved it by creating outlets for the constraints separating the labels (in my case, margin was fixed).
I did:
let constant = (constraint1to2.constant + constraint2to3.constant)/2
constraint1to2.constant = constant
constraint2to3.constant = constant
Upvotes: 0
Reputation: 119292
There are a couple of ways to approach this.
Upvotes: 1
Reputation: 6032
You could wrap big percent label and corresponding detail label into a wrapper view and make that wrapper view be sized with it's content, then just lay this three wrapper views in a line with the 20px paddings.
-.-.-.-. -.-.-. -.-.-.-.
- (20) -| 100% | - (20) - |100%| - (20) - | 100% | - (20) -
|detail| | d | |detail|
-.-.-.-. -.-.-. -.-.-.-.
That's just one of the solutions, straightforward one.
Upvotes: 0
Reputation: 113777
There's an example for creating equally-spaced views in the Auto Layout Programming Guide. The method is to create invisible "spacer" views between the views you want spaced (and also on the edges) and set the spacers widths using Auto Layout. The width (or height for vertically spaced views) of the spacers are set to be equal. From the PG:
To space views proportionally
1. Create the visible views.
2. Create the spacer views equal to the number of visible views plus one.
3. Alternate placing your views, starting with a spacer view.
To space two visible views, place all of the views in the following pattern, starting from the left side of the screen and moving right:
spacer1 | view1 | spacer2 | view2 | spacer3.
4. Constrain the spacer views so that their lengths are equal to each other.
5. Create a leading constraint from the first spacer view to the container view.
6. Create a trailing constraint from the last spacer view to the container view.
7. Create constraints between the spacer views and the visible views.
Upvotes: 5