Contentsize setting of UIScrollerview, contentsize

Source: Internet
Author: User

Contentsize setting of UIScrollerview, contentsize

Recently, a friend from the same company asked a question: "Do I need to reset contentsize for scrollerview after adding a child control to UIScrollerview to constrain the layout of the Child control ?" As a result, I thought of a project that I had previously started. I used scrollerView and added a sub-control to it. I remember that the frame of scrollerView was the same as that of the screen, contensize is re-set after the child control layout in it is completed, so my answer is to set it. But at last, the friend told me to let me go back and try again, saying that there is no need to set it. It also said that it may be because I have different requirements with him. I need to reset the contentsize. Finally, I tried it and sorted it out as follows.

First, use storyboard to create a UIScrollerview to the current view, set the spacing between the upper and lower sides, and update the layout. At this time, it is certainly no problem and displayed normally. Then, put another UIImageview on the scrollerView, and the spacing from top to bottom to the scrollerView. Update the constraints. The layout error is displayed. It is reasonable to say that the normal control can work normally. The key to the problem is how to add complete constraints to the child controls in scrollView. To add constraints to the child controls in scrollView, two principles must be followed:

1. The size of the Child control in scrollView cannot be referenced by the size of scrollView.
2. Constraints of child controls in scrollView must be complete

First, the size of the Child control cannot be referenced by the size of scrollView, so we have two options:

  • Provide a specific value constraint (such as 200)
  • The size of the sub-control can refer to the size of controls other than scrollView (for example, the size of the view of the Controller)

Second, the constraint "integrity" means that the horizontal and vertical constraints of the Child control should fill the scrollView ".

In other words, in the horizontal direction, we need to set:

  • Distance between the left-side control and the parent control of the Child Control
  • Width of the Child widget
  • The distance from the parent control to the Right of the Child control.

The vertical direction is also the same, you need to set:

  • The distance from the parent widget to the top of the Child widget
  • Height of the Child widget
  • The distance between the child control and the parent control at the bottom.

The problem above is that the UIImageView size is not set.

Why is scrollView so special?

This is because scrollView needs to calculate the width and height of the Child control added to it and the distance from the surrounding area.contentSize.

For example:
The width and height of an imageView added inside the scrollView are {80, 50}, and the imageView distance from the top left to the bottom right is 100,200,300,400, respectively. Therefore, you do not need to assign contentSize values using the code, we can print the contentSize of scrollView to {680,450 }.
:

 

That is to say,The constraints of the Child control determine the size of the container (contentSize).

Here, you can use storyboard for the test. You do not need to set the contentsize at all. However, the problem is found in SDAutolayout, a commonly used layout framework. The contentsize is always the initial screen size, finally, I tried it again using the layout framework Masonry. At this time, it was okay. Well, here I can only say that this problem still exists in SDAutolayout. The layout of my original project scrollerView is very useful SDAutolayout, the Layout Framework is clearly a problem. Please pay attention to it !!

Original: http://blog.csdn.net/u014795020/article/details/51893943

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.