iOS使用xib创建scrollview
2023-04-25 本文已影响0人
iOS_ITCode
一、添加scrollview
拖入scrollview ,去掉右边栏Content Layout Guides,设置上下左右约束为0

结果如下

#二、拖入UIView到scrollview下,改名为contentview

三、设置contentview相对于scrollview的约束(选中contentview,右键拖线到scrollview),设置上下左右约束都为0及居中约束(这里有一个细节,如果你的scrollview是上下滑动,则居中约束设置为Center Horizontally in Container,如果是左右滑动则居中约束设置为Center Vertically in Container,如果左右上下都要滑动则不需要设置。本文以上下滑动为例子,即设置Center Horizontally in Container约束)如图

注意:接下来就是布局你的视图了,到目前为止还会出现红色警告。
#四、拖入你想要布局的view到contentview下,这里以两个view为例子,重点注意:最后一个view要设置Bottom Space to Container约束。
1.设置FirstView相对于父视图(即contentview)上左右约束,设置FirstView相对于LastView的vertical spaceing约束,设置FirstView的高度
2.设置LastView相对于父视图(即contentview)下左右、高度约束。如图,绿色的为FirstView,白色的为LastView。这里需要注意的是,LastView相对于父视图(即contentview)的下约束(即bottom space to Container)决定了scrollview滑动的距离。可以试试改为1000试一下,改完之后选中contentview然后滑动鼠标上面的滚动按钮,xib里面的视图也会跟着滚动
