Explain the frame, bounds and center properties of UIView in detail

1. Overview
Open ios official development documentation , and find that the explanations of these three attributes are as follows:
  • frame: Describes the position and size of the current view within its superview.
  • bounds: Describes the position and size of the current view in its own coordinate system.
  • center: Describes the position of the center point of the current view in its parent view.

Are you a little confused? Well, don't worry, let's talk about the iOS coordinate system first.

2. The ios coordinate system

ios takes the upper left corner as the coordinate origin (0,0), and the right side of the origin is the positive direction of the X axis, and the lower side of the origin is the positive direction of the Y axis.

ios uses CGPoint to represent the point on the coordinate system X, Y Location. We can create a coordinate point through CGPointMake(x,y): CGPoint point = CGPointMake(80,40)

At the same time, ios uses CGSize to represent the width and height of the view, that is, the size of the view. We can use CGSizeMake(width,height) to create a rectangle size, such as CGSize size = CGSizeMake(144,72) will create a rectangle with a width of 144 and a height of 72.

CGRect is a combination of CGPoint and CGSize to represent the position and size of the rectangle. Its origin represents the position of the upper right corner of the rectangle (CGPoint), and size represents the size of the rectangle (CGSize).

3. The difference and connection between frame, bounds and center

Returning to frame and bounds attributes, it is not difficult to find through the previous description that these two attributes are used to describe the size (CGSize) and position (CGPoint) of the view. Both are represented by CGRect. The difference is that frame describes the CGRect in its parent view, while bounds describes the CGRect in its own view, that is, the coordinate systems where the two are located are different. View B is a subview of View A, then, the frame property of View B is origin (200, 100), size (200, 250), and the bounds property of View B is origin (0, 0), size (200, 250).

The center attribute uses CGPoint to represent the position of the center point of the rectangle in its parent view. As shown in Figure 3, the center attribute of View B is (300, 200).

The three attributes of frame, bounds, and center are interrelated and affect each other. When one of the attributes changes, the other attributes will also change.

4. The usage scenarios of frame, bounds and center

Generally speaking , the bounds property is rarely used. Usually, frame is used to set the size and position of the view, and center is used to change (move) the position of the view (the frame can also be used to change the position). In addition, the rotation and scaling of the view are also performed relative to the center.
From: http://unmi.cc/ios-bounds-frame

frame and bounds are two properties in UIView.
The frame refers to: the position and size of the view in the parent view's coordinate system. (The reference point is the parent's coordinate system)
bounds refers to: the position and size of the view in its own coordinate system. (The reference point is its own coordinate system)

-(CGRect)frame{
  return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
}
-(CGRect)bounds{
  return CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326476052&siteId=291194637