Using the AutoLayout layout in Uiscrollview (2)

Source: Internet
Author: User


In the use of AutoLayout layout in Uiscrollview (1), we present a simple example of adding 2 controls that use AutoLayout layouts in Uiscrollview.

The reality of the situation is of course more than that simple.

We use Uiscrollview, which is usually a control that needs to put more than one screen size on top.

What happens if you add a lot of labels, textfield, or other controls as per the steps (1)?

You will notice that the Red Layout error will appear again, and the AutoLayout layout engine warns that the Uiscrollview's contentsize width or height cannot be calculated because the condition is insufficient.

This is because we violated the first rule: Uiscrollview's contentsize relies on subviews

A large number of subviews use the top constraint and the leading constraint. These constraints are relative to the Superview. And what is Superview? is actually uiscrollview.

This makes the layout engine very contradictory, how to calculate contentsize? To calculate the contentsize must be clear each subviews frame, and subviews frame incredibly rely on ScrollView?

Therefore, the condition of calculating contentsize is insufficient.

You can run the program without ignoring the layout error. However, contentsize cannot calculate the result, which causes the runtime Uiscrollview not to scroll.

So when you find out that Uiscrollview can't scroll, it's often because we violate the first rule and cause the contentsize calculation to be wrong.

The correct steps are as follows:

1, drag into a uiscrollview.

2, set the Uiscrollview constraints. For example, the pin up, down, left, and right margins are all 0, causing Uiscrollview to occupy the entire view.

3, drag into a uiview, we might as well name it as Contentview. Constrain the width and height of the contentview so that he can meet your layout needs and be able to lay down all the controls you want to place (typically more than the size of the screen). For example: Width equals Viewcontroller ' s view of Width,height equals 800.

4, then important to come , set uiview pin constraints, up and down around 0. Why? Clearly set the width and height of ah, Uiscrollview can not calculate the contentsize it? Believe me, if you don't do this step, I guarantee your uiscrollview still can't scroll. These 4 pin constraints are actually used to tell Uiscrollview: I'm going to put your content "up to" how big. In other words, contentsize must be determined according to the 4 edges of the uiview, rather than by any other method. In this way, Uiscrollview the Contentsize 4 side and UIView of the 4 border linked up. If the UIView 4 changes, you do not need to say that the Uiscrollview will automatically adjust the contentsize.

5, the rest of the matter is irrelevant. lay out your controls on this uiview, and they will all be properly laid out. For example, we put a label (constraint: Top, leading) in the upper-left corner of the UIView, and placed a label (constraint: Bottom, trailing) in the lower-right corner. Of course, these constraints are related only to Contentview, not to Uiscrollview. Otherwise, it will cause layout conflicts.

To show you a picture will be more assured that the point:

Open the emulator, run the program, and see only the first label. Drag the ScrollView and you'll be able to see the 2nd label.

Using the AutoLayout layout in Uiscrollview (2)

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.