1 view contoller的初始化
initWithFrame: self.view.bounds or self.view.frame or CGRectMake(0, 0, 100, 100)
bound和frame是两个结构体变量:
struct CGRect { CGPoint origin; CGSize size; }; typedef struct CG_BOXABLE CGRect CGRect;
//--------------------------------------------
struct
CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CG_BOXABLE CGPoint CGPoint;
//---------------------------------------------
struct CGSize {
CGFloat width;
CGFloat height;
};
---------------------------------------------
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x; rect.origin.y = y;
rect.size.width = width; rect.size.height = height;
return rect;
}
关于Frame和bounds:
Frame
As the documentation clarifies, the frame
of a view is a structure, a CGRect
, that defines the size of the view and its position in the view‘s superview(依赖父view), the superview‘s coordinate system. Take a look at the following diagram for clarification.The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.
Bounds
The bounds
property of a view defines the size of the view and its position in the view‘s own coordinate system. This means that in most cases the origin of the bounds of a view are set to {0,0}
as shown in the following diagram. The view‘s bounds
is important for drawing the view.The bounds rectangle, which describes the view’s location and size in its own coordinate system.
This means a few things:
- If you create a view at X:0, Y:0, width:100, height:100, its
frame
andbounds
are the same. - If you move that view to X:100, its frame will reflect that change but its bounds will not. Remember, the bounds is relative(相对) to the view’s own space, and internally to the view nothing has changed.
- If you transform(改变) the view, e.g. rotating it or scaling it up(旋转缩放), the frame will change to reflect(反映) that, but the bounds still won’t – as far as the view is concerned(关注) internally, it hasn’t changed.
- When you change the width or height of either
frame
orbounds
, the other value is updated to match. Generally it’s better to modifybounds
pluscenter
andtransform
, and let UIKit calculate theframe
for you.
CGRectMake 设置原点和宽高,但是坐标系要看它是附在哪个view上。
setBounds的作用是:强制将自己(view1)坐标系的左上角点,改为(-20,-20)。那么view1的原点,自然就向在右下方偏移(20,20)