Use AutoLayOut to add constraints and highlights for UIScrollView.
1. Drag a UIScrollView into ViewController and add constraints to it.
The constraint is alignment with superview on the top, bottom, left, and right sides.
2. In scrollview, drag a UIView to set it to orange for convenience.
3. Add constraints to the view
The constraint is alignment with superview on the top, bottom, left, and right sides.
After adding these constraints, we found an error. ignore this.
4. Add a width constraint for the view. In this example, scrollView can be scaled vertically instead of horizontally,
Therefore, you can constrain the view width to be the same as that of scrollView.
5. Add a height constraint for the view.
Update frame
Pleasant discovery of constraint errors has disappeared!
To verify that the scrollView slides correctly
We add a Blue view to scrollView, and add the width and height constraints to 60,500 respectively, and then add the left margin and top margin of the yellow view (that is, the superview of the Blue view ).
Now, all constraints are added. You can add the content to be rolled in the yellow view.
Click Run to view results
You can see that scrollview slides as expected.
Finally, for all the constraints, see
Key points for adding constraints to UIScrollView:
1. The superview constraint between scrollView and scrollView is used to determine the frame of scrollView.
2. The Constraint Relation of "contentView" in scrollView (Blue view in this example) is used to determine the contentInsets of scrollView.
3. The width and height of contentView are used to determine the contentSize of scrollView.
Understanding these three points makes it easy to add constraints to the scrollView.