I've created a simple view setup in interface builder. Here's how it looks:
The view hierarchy is simply:
view
- scrollView
-- label
The scroll view is shown with grey
background anchored to its super view top, leading, trailing, and bottom with constraints of 0.
The label is shown with yellow
background and has constraints as shown. Additionally, the label has content hugging priority
of 1000
for both horizontal and vertical, and it has content compression resistance priority
of 1000
for both horizontal and vertical.
In portrait orientation, the label is sized correctly:
In landscape orientation, however, the label is not sized correctly horizontally (I intended for the label to fill the width of the screen, less constraint insets as shown):
How can I get this label to size correctly horizontally in landscape orientation?
There is one solution for you.
1. Add your UIScrollView to container (UIView) with zero constraints:
2. Add constraints for Label: top, bottom, leading, trailing spaces = 20.
3. Add constraint: label.width = container.width - 40
You should get the following components:
Run the app, go to landscape and it works!
Hope it is clear. Best Regards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With