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