Due to the complex characteristics of ScrollView itself, such as contentsize and contentinsets, it is very different from normal view in the automatic layout. To automate the layout of the ScrollView, you need to make a contentview as a scrollview scroll container. Here are the steps to automate the layout of ScrollView under IB:
First, create a basic view controller interface in Xib or storyboard (This step does not say) two, layout ScrollView
Drag a scrollview into the interface and set the desired area, fixed up or down:
Third, the layout contentview
Drag out a view (depending on the situation, this page may be used to display plain text, then just drag a label on it) as a child view of the Scrllview. This view will be used as the ScrollView Contentview to determine ScrollView's presentation. First, the upper and lower sides of the Contentview are fixed:
After the discovery will error, said Contentview the length of the width unknown. For normal view, fixed up and down left and right interval after the natural no longer need to set the length of the width, why still error? This is the difference between scrollview. Contentview as the content content of ScrollView, on its fixed up and down around the interval, in fact, only to set the content view and edge spacing function, just this role, the code and set the role of Contentinsets is the same! Therefore, the length and width of the contentview is not recognized. Apple has made a special deal with this step of ScrollView. So next, we're going to set the length of the Contentview.
Iv. Setting the Reference view
For Contentview, we have to constrain its length and width, in fact in code we also set the contentsize to the same effect. However, if you do not want to fix the long width, you need a reference view to determine the length of the width, because you can not scrollview to determine its length and width. So at this point, drag a view as a reference view and automatically layout it:
V. Setting Contentview length and width
Based on the reference view, the Contentview's length is automatically laid out. You can set the width of the two, the height wants to change dynamically, first set a value, and fixed the height, and then drag the height constraints into the code, through the code to dynamically change the height of the Contentview, that is, the ScrollView scroll height:
Pre-preparation is complete, can be a grand exhibition!
All of the next controls are laid out on contentview, just like normal views!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development-uiscrollview Auto Layout