The difference between frame and bounds

Source: Internet
Author: User

Today when using iOS Uicollectionview, you need to add a child view of the label type on each item on CollectionView. Each line in the code is set to display seven item.

At first, I added Subview in the following way:

[[UILabel alloc] initWithFrame:self.frame];

The results of the run are as follows:

As you can see, only four of the labels are displayed.

However, if you change the label's initialization code to:

[[UILabel alloc] initWithFrame:self.bounds];

The result of the operation is normal, such as:

So what is the difference between frame and bounds?

As you can see from the above two graphs, the coordinates of the label of the first picture are clearly beyond the scope of item. Frame refers to the coordinate system relative to the parent view. Therefore, each time a label is initialized, it is the X coordinate of item relative to item's X coordinate, and the X coordinate of item increases with the increase of item, so the relative coordinates of label are increasing, so the range of item is offset.

and bounds refers to the coordinates of a view relative to itself. By default it is (0,0), so when you add a label child view to an item, you should use bounds, which will not cause an offset.

is the frame and bounds Relationship diagram:

The difference between frame and bounds

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.