Is the UILabel size right inside the UIScrollView using Interface Builder and Autolayout?

I created a simple view setting in the interface builder. Here's what it looks like:

IB View Layout

The hierarchy of representations is simple:

view - scrollView -- label 

The scroll view is displayed with a grey background, tied to the top, top, bottom, and top rows from above with 0 restrictions.

The label is displayed with a yellow background and has limitations as shown. In addition, the label has a content hugging priority of 1000 for both horizontal and vertical, and has a content compression resistance priority of 1000 for horizontal and vertical.

In portrait orientation, the label has the correct size:

Portrait Orientation Screenshot

However, in landscape orientation, the label has no horizontal size (I wanted the label to fill the width of the screen, there are less restrictions, as shown):

Landscape Orientation Screenshot

How can I correctly assign this label horizontally in landscape orientation?

+3
source share
2 answers

There is one solution for you.

1. Add a UIScrollView to the container (UIView) with zero restrictions: enter image description here

enter image description here

2. Add restrictions for the label: upper, lower, front, trailing spaces = 20.

3. Add restriction: label.width = container.width - 40

  • To do this, select the label in the tree of the presentation structure, press ctrl and drag to the container. And select "Equal Widths."

enter image description here

  • Then select the created restriction and go to its Utilities and set the constant value to 40.

enter image description here

You should get the following components: enter image description here

Launch the application, go to the album and it will work!

enter image description here

I hope this is clear. Best wishes.

+11
source

It is difficult to say which restriction (s) to add / remove in order to get what you want, because iOS reserves the right to adjust your restrictions when it becomes impossible to satisfy all your restrictions.

I am making an empty project with the same view ( UILabel as a subview of UIScrollView ) and creating some restrictions for the correct resizing of the UILabel in the landscape.

enter image description here

Be sure to check:

  • Make sure you set the vertical / horizontal limits between the output option, as shown below, and try to remove the unnecessary limits manually.

enter image description here

-3
source

Source: https://habr.com/ru/post/977358/


All Articles