Using the AutoLayout layout in Uiscrollview

Source: Internet
Author: User

Previously translated a "How to use Uiscrollview (multiple Contentview)" in AutoLayout (hereinafter referred to as "how"). The limitations and considerations for using automatic layouts in Uiscrollview are explained in detail in this article.

I thought it had been explained very clearly. However, there are still readers who say that although the example is feasible, the same effect cannot be done on other interfaces.

Considering that perhaps the case is too complex, so that some readers can see, although the steps in the text will be able to make an effect, but because there is no real understanding of the original intent, once left the example to achieve their own UI is puzzled.

We also give a simple example to fully understand the basic principles used by Uiscrollview under AutoLayout.

There are a number of principles listed in the article, but the most fundamental is the following two articles:

The principle 1:uiscrollview size depends on the subviews

First, drag a uiscrollview into the canvas. Use the pin button to freely set its layout. Not even set.

Because the first principle in the article "How to" is that Uiscrollview's layout relies on subviews. Therefore, it is useless for us to set the layout constraints of Uiscrollview. We only need to clear the size of the subviews, this principle is naturally not a problem.

The size of the Uiscrollview (that is, contentsize) is calculated according to the size occupied by the subviews. Of course, if the content of contentsize is not enough to fill the entire uiscrollview, the scrollbar will not appear and Uiscrollview will not scroll.

The principle 2:subviews size cannot depend on Uiscrollview

For example, you design a layout like this:

The Uiscrollview consists of two Subview:uilabel and Uitextfield.

The PIN constraint settings for Uilabel are as follows:


Note that we fixed the uilabel width to 100, and the label's height was fixed (lines set to 1). This is possible because its width and height are not dependent on the Uiscrollview.

But we can't use a fixed width for all the controls, so it's not called "auto-layout."

For example, Uitextfield, we are ready to set its PIN constraint to:


In this way, the Uitextfield has no fixed width and height. This is not right, in fact, Uitextfield is fixed high, for a use of the default border type (fillet) of the Uitextfield, its height is fixed to 30. However, the width is determined by the left and right edges.

It is known that its left edge is 8 pixels from the left Uilabel and the right edge is 14 pixels from the right edge of Superview. So the width of the Uitextfield should be obtained by the X of the right edge minus the X on the left edge.

Because the left Uilabel is fixed width, the left edge position of the Uitextfield is also fixed. But the X on the right edge is relative to the Superview, which is impossible to fix.

We know that in Xcode6 's adaptive layout, the size of the view is actually changed depending on the device, a size on the iphone, and another size on the ipad.

However, our Uitextfield can be adapted to whatever device it is. It's just a little narrower on the iphone and a bit wider on the ipad.

But when you set such a constraint, you'll notice a red layout warning appears:



This means that the width of the Uitextfield is much shorter at run time than the layout. The results are as follows:


This is not what we want to look like.

This is because Uitextfield violates the principle of 2:subviews size cannot depend on Uiscrollview.

As mentioned above, the width of the Uitextfield depends on the width of the superview. But unlike what you understand, Uitextfield's superview is Uiscrollview rather than viewcontroller view.

Why is subviews size not dependent on Uiscrollview?

Very simple, because the principle 1:uiscrollview size depends on subviews. If the size of subviews is again dependent on Uiscrollview, the layout engine becomes chaotic. Just as we solve an equation, the solution of X is to know the value of y first, and the value of Y must first clear the value of X, this is no solution.

To solve this problem, we need to clear the width of the Uitextfield, at least not with the uiscrollview size of the unreliable. We can set the width constraint of the Uitextfield in this way:

Locate the Uitextfield size panel, double-click the tailing Space to this constraint (if you click just unload instead of delete):


Then press the DELETE key.

Right-drag from Uitextfield to Viewcontroller view, then select equals Widths. Then double-click on the newly created constraint and modify it to look like this:


Note that the first item and the second item do not have to be reversed.

To run the program:

The Uitextfield on the iphone and ipad are well adapted.


For the height of the layout display is the same, I do not give an example. In short, the above principles must not be violated.


Using the AutoLayout layout in Uiscrollview

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.