It is a little complicated to explain.
As we know , before the autolayout
get working, we should set constraints on them , according to those constraints the compiler can determine the Frame
(position and size) on controls.
However, UIScrollView
is a bit of different from UIView
. In general, we just need to set left,right,top,bottom
, but in UIScrollView
, it has a definition called contentSize which determines the actual size of scrollview. so the constraints we set above is not enough.
So, as a workaround ,we add a container view inside the scrollview to determine contentSize.
1. Constraint on scrollview (green area in the following gif)
![enter image description here](https://i.stack.imgur.com/IZ1h4.png)
2. Constraint on container view.(red area in the following gif)
pay attention: we set additional width (equal to scrollview) in order to indicate the the width is fixed , means we can't scroll horizontally but vertical orientation is allowed.
![enter image description here](https://i.stack.imgur.com/v6aES.png)
3. Constraint on inner view(blue area in the following gif)
![enter image description here](https://i.stack.imgur.com/R1nIp.png)
Test result :
![enter image description here](https://i.stack.imgur.com/NmpSy.gif)
iPhone5S
![enter image description here](https://i.stack.imgur.com/dmy8w.png)
iPad Air2
![enter image description here](https://i.stack.imgur.com/VmHrx.png)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…