Search code examples
dynamicios6labelorientationautolayout

How to create labels with dynamic size in iOS 6 with autolayout


I'm having trouble with autolayout and labels that get their text set in the source code based on some external conditions. These layouts (portrait and landscape) look like this on the simulator:

https://i.sstatic.net/YkRnK.jpg

https://i.sstatic.net/OayNI.jpg

The second one is obviously not what I want it to be, the label with the URN should fill the whole screen. I have a width constraint for the dynamic label added by Xcode which I can't get rid of even though the "Name"-label has a fixed width and all horizontal spaces are fixed as well which should logically rid me of the need to have a width constraint for the label containing the dynamic text. Any help on how to achieve what I want? Thanks in advance!


Solution

  • It looks to me like your labels are not binding their trailing edge to the superview. In the lower right there is the little 'I beam' icon, click on your label and then that icon, the top of the menu should be 'Pin' and pick trailing edge to superview. The label should expand to fit the width of the screen, minus some padding. Once that is in place you should be able to remove the width constraint - although you may need to find it in the size inspector and select promote to user constraint before you can remove it.

    If you don't see the pin menu, try going up to the top and picking Editor -> Pin and you should see it there.

    If it is already pinned, try increasing the content compression resistance priority and decreasing the priority of the width - although if pinning it does not fix it then there is another constraint at play.