UILabel will not wrap words in a UIScrollView using AutoLayout

I have a UILabel inside a UIScrollView that I am trying to describe. I want to use AutoLayout to configure the layout. The word UILabel well- UILabel when it is not inside a UIScrollView . I just need to set the number of lines to 0 and set the line break mode to word wrapping.

However, when I put a UILabel inside a UIScrollView , word wrap stops working. I already checked " Dynamic UILabel height inside UIScrollView with AutoLayout " and " Correct UILabel height inside UIScrollView using Interface Builder and Autolayout? ", But none of these questions give answers that help me.

I have set up a test project that accurately reflects what I'm talking about if someone wants to take a look at it.

+6
source share
3 answers

The reason is that the limits inside the scroll attached to the scroll view do not mean that you think about them. They are related to the definition of contentSize from inside view; they themselves do not determine the size of things inside the scroll from the outside. See my answer here: fooobar.com/questions/75507 / ...

The simplest solution in your case is to use an intermediate representation of the content, so you have this hierarchy:

 scroll view content view label 

Now give the absolute restrictions on the width and height of the content view and snap them on all four sides to the scroll view. Now the shortcut will work as you expect. The presentation of the content has sufficient limitations both to set its own size and to limit the viewing of contentSize scrolling from the inside. Meanwhile, label restrictions do not apply to the scroll view, but to the content view, and since the content view is fixed in size and is a regular view (and not scroll), the label constraints do what you expect.

+10
source

I had the same problem, but I was able to get the correct UILabel shell by setting the following label restrictions

  • Leading surveillance space
  • Trailing space for observation
  • Equal Width to Superview ** (this is the one that limits the width on the label so that it completes correctly)

You should also have restrictions leading from the top of the scroll view. For example, if you only have a shortcut, you must install Top Space and Bottom Space for Superview. If you do not, it will have an ambiguous height of the scrollable content.

To prevent horizontal scroll scrolling, check the "Direction Lock Enabled" box and call setContentOffset correctly inside the scrollViewDidScroll method (see the first answer for this question for more details).

+6
source

Try using UILabel's preferredMaxLayoutWidth and set the parent view width for it.

+1
source

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


All Articles