In xcode6, how does one automatically layout scrollview (autolayout) and xcode6scrollview

Source: Internet
Author: User

In xcode6, how does one automatically layout scrollview (autolayout) and xcode6scrollview

Xcode6 greatly enhances the ability of Automatic Layout in IB. Next we will conduct an automatic layout practice for the spider's scrollview to see whether the value of Automatic Layout in Xcode6 is worth using:

UIScrollView is a spider, and it is not exaggerated. This is caused by complicated features such as scrollview contentSize and contentInsets, when talking about autolayout, Apple documents even come up with a special section on how to automatically layout the scrollview. A foreign guy once asked Apple employees about the layout problem he encountered. It took 40 minutes to come up with a feasible solution:

When I had a chance to go talk to an actual Apple Engineer about AutoLayout last week at WWDC, I made sure to go. I thought of my most painful experience using AutoLayout recently-when I was making a login screen with username and password fields on a ScrollView (so it scrolls up when the keyboard comes up) -and had the Apple engineer walk me through the example ....... this "simple" solution took The Apple Engineer 40 minutes to solve! However, several senior engineers I know said that they 've never been able to get AutoLayout working quite right on a ScrollView, so 40 minutes is actually not bad! [Original address]

1. What are our requirements?

We need to implement such a layout:


The entire view has three components: a portrait (UIimageView), a text (UILabel), and a UIScrollView. Our layout is expected to adopt a normal upper and lower structure for the entire view, while the left and right structures for the horizontal screen.

2. For the convenience of layout, for multiple view components with close logic or structure, we usually put multiple views in a containerview, in this example, the parent view is isolated from the external view to cope with external changes. In this example, the profile picture and text label are put into a contanierview. In this way, the main work of our layout is concentrated between the two components: containerview and scrollview where the Avatar is located. Similarly, for the sub-view in scrollview, we can also place it in the same parent container view, and then use this container view as the sub-view of scrollview, that is, content view, in this way, the layout of the scroll view can be simplified to the layout of the content view. The layout of the Child view in the content view is a normal layout compared to the content view, the rest only needs to solve the layout of scroll view and content view. Open storyboard and first layout the default size class (wAny, hAny) (for details about the use of size class, see my previous blog ), add four constraints to the iner view of the Avatar to determine its frame (x, y, width, height). These four constraints are: horizontal center, fixed length from the top, fixed width, fixed length, basic layout no more words, nothing more than click, select, set ...... for details, see Apple official document. For layout analysis, see:
The next step is to layout the scrollview. We know that in addition to its own layout, the scroll view needs to be considered (x, y, width, and height, another contentSize attribute must be determined during the layout process. contentSize is the size of the content to be displayed by UIScrollView, in the layout, this contentSize is actually the sub-view of scroll view: width and height of content view. Note: We cannot set the width and height constraints of the content view to be determined by the scroll view.(Such as the width and height of the scroll view). Otherwise, Xcode will warn: The content size of the scroll view is unknown!


In this case, we must introduce other references except the scroll view to the layout constraints of the content view. We drag in a secondary view as the reference or anchor, as shown below:

In storyboard, the three view levels are:

Through this reference view, determine the width and height of the content view. Although the size of the content view does not depend on scroll view, we have to set the relationship between the content view and its parent view: specifically, it is necessary to determine the top, bottom, leading and trailing contstraints of the content view and scroll view, which may be confusing, the reason is that Apple's use of these four constraints has changed in the scroll view: it is no longer the basis for determining the size of the content view, in the scroll view, the boundary around the content view (or you can understand it as white) is used to determine the contentSize attribute of the scroll view.

In this way, the layout of the default size class is completed. (Note: the layout of the Child view of the content view is not described here ). 3. horizontal screen layout for ease of layout, we first adjust the size of the view in the storyboard to 667*375, the size of the iphone 6 horizontal screen, then, set the height of the size class to the compact mode (because the height of the horizontal screen is in the "COMPRESSED" State), so that we can separately layout the status of the horizontal screen, xcode uses the size class for automatic layout, which greatly improves flexibility (in my previous blog). Constraints of different size classes are isolated from each other, even the view added under another size class is invisible (not installed), which greatly simplifies the layout.



After adjusting the view size, set the size class to (wAny, hCompact)

As mentioned above, in the Landscape mode, we hope to arrange the portrait and scroll view in the order of left and right so that the screen space can be effectively utilized, provide users with the best user experience (especially after iPhone 6 & plus is released ).

Considering the landscape screen, we do not know the specific width of the screen (actually there may be four values from 4s to 6 plus). We want to determine the specific position of the Avatar and scroll view, we have to look for another reference object. With reference, we can determine the constraints. This idea of reference objects is widely used in automatic layout, which can effectively help us reduce the complexity of layout and improve the flexibility of layout.

We still select a view as the reference object or anchor. We place it in the middle of the screen. This view is called middle anchor view, the trailing space of the Avatar and the leading space of the scroll view can be used to determine the x coordinate value with the middle anchor view as the anchor.


Next, you can add other constraints of the Avatar, scroll view, and content view in sequence based on the vertical screen. After the layout is complete (the red view is anchor view, which can be hidden after the layout is complete ):


Note that we used the arbitrary size class (wAny, hAny) for the width and height of the front vertical screen, which contains (wAny, hCompact). Therefore, the layout elements set in the previous size class still exist in the current size class. To prevent layout interference, we can clear all these la S and re-layout them:


You can easily clear all constraints on the storyboard.


After the layout is complete, compile and run the program to obtain the effect shown in the preceding requirements.

Iv. Summary 1. before layout, consider what the requirement is and what the UI display effect is when the screen is horizontal or vertical; 2. place its subviews in a content view to simplify the layout. when layout the content view, it must attract a third-party view. The size of the content view has been determined. Note that the size of the content view cannot be determined based on the scroll view. 4. make sure to set the content view's top-bottom (top, bottom, leading, trailing) Spacing relative to the scroll view. These settings help the scroll view to determine its contentSize instead of determining the size of the content view; 5. pay attention to the use of some tips during layout, and get twice the result with half the effort:. the thought of putting multiple views into one container view is reduced to zero. B. introduce a reference object or anchor to assist in positioning and layout;


In IOS development, how does one change the contentsize value of scrollview under the layout restriction?

The autolayout option is selected.
In autolayout, The contentsize calculation mechanism of scrollview in ios is slightly different.

Under autolayout, The contentsize of the scrollview is recalculated based on the constraint of the subview before viewDidAppear. This is why it is useless to manually set the contentsize in viewdidload. Because it will be re-calculated later, and the previously manually set value will be overwritten.
Solution:
1. Remove the autolayout option and manually set the contentsize.
2. If you want to use autolayout, you can either set the constraint of the subview, and then let the system automatically calculate the contentsize Based on the constraint. You can also manually set the contentsize in viewDidAppear.

How to implement animation when using AutoLayout

Remove the constraint first, then add the constraint, and then layoutIfNeeded
 

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.