1、第一步,创建ScrollView,取消勾选Content Layout Guides,设置上下左右的约束为0,如下图所示
创建ScrollView
2、第二步,创建UIView至ScrollView,并改名为contentview,然后设置约束
(1)设置contentview相对于scrollview的约束(选中contentview,右键拖线到scrollview),设置上下左右约束都为0及居中约束
(2)如果你的scrollview是上下滑动,则居中约束设置为Center Horizontally in Container,如果是左右滑动则居中约束设置为Center Vertically in Container,如果左右上下都要滑动则不需要设置。
(3)本文以上下滑动为例子,即设置Center Horizontally in Container约束)
设置contentview约束
3、第三步,创建两个UIView(可以为多个,最后一个view要设置Bottom Space to Container约束),添加至contentview,接下来设置两个view的约束
(1)这里为了讲解,将第一个视图叫firstView,第二个叫lastView
(2)设置firstView相对于contentview上左右约束,设置firstView相对于lastView的vertical spaceing约束(下图firstview中的bottom space to:lastView),设置firstView的高度
(2)设置lastView相对于contentview下左右、高度约束。lastView相对于contentview的下约束(即bottom space to Container)决定了scrollview滑动的距离。可以试试改为900试一下,改完之后选中contentview然后滑动鼠标上面的滚动按钮,xib里面的视图也会跟着滚动
firstView约束
lastView约束
到这里就创建成功了,如果想多个view,记住在最后一个view上设置相对于contentview的下约束,这个决定了scrollview滑动的距离。
到这里就结束了,有问题请指出~