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)?

戏子无情 提交于 2019-12-04 16:33:23

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/19470156/using-auto-layout-how-do-i-make-two-labels-on-the-same-line-be-dynamic-in-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!