Transferred from: http://dadage456.blog.163.com/blog/static/30310744201491141752716
1, create a blank Uiviewcontroller2, add Uiscrollview to the UIView control, and set Uiscrollview constraints for the parent view UIView (Leading/trailling/top/bottom =0)3, the most important comes, add UIView controls to Uiscrollview ... (1) and renamed Contentview .... (Uiscrollview is mainly based on sub-view to achieve AutoLayout height and width of the calculation) (2then set the Contentview for the parent view Uiscrollview constraints1, leading/trailling/top=0 2, bottom =1 3, Equal height/equal width (contentview height and width equal to Uiscrollview)4, set equal height priority is less than the priority of the bottom just set (the bottom is contentview for uiscrollview constraints), here set priority=999.
Need attention First: why bottom=1rather than bottom =0。 In real-world development, if it is 0 and the content in Contentview does not exceed the height of the screen, the Uiscrollview cannot be pulled down.
Second: To achieve the height of the Contentview to the height of the Uiscrollview contentsize, need to contentview sub-views have a clear top and bottom, Used to let AutoLayout calculate the actual height of the Contentview.
Third: Why to set the priority of equal height ... Let's write a formula that's easier to understand because Scrollview.height=Uiview.height again because Contentview.height= Scrollview.height +1so Contentview.height!=Scrollview.height is the inability to achieve the equal height of this constraints. Will error. Just adjust the priority to solve this problem, conflict, priority to adapt to the height of the contentview .... Now you can add the controls you want to add on the Contentview, but remember that if the control you want to add is already beyond the screen, you need to add bottom and top (no more than you can) to the contentview inside the child controls. In this way AutoLayout can calculate the height of the Contentview, and assign this height to the Uiscrollview contentsize, to achieve the adaptive height ... PostScript: Contentview If use Uilabel, to give Uilabel clear height constraint, so that auto layout can calculate the height.
Uiscrollview using AutoLayout to scroll vertically