Use AutoLayOut to add constraints and highlights for UIScrollView.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.