更新时间:2023-11-24 14:15:04
假设您已初始化自定义UIView
,则需要将其添加为viewController
视图的子视图.
Assuming you have initialized the custom UIView
, you need to add it as a subview of the viewController
's view.
-(void)addSubview:(UIView *)view
例如,如果您有一个名为myVC
的普通viewController
,它的视图只是空白的白色UIView
,您会这样说:
So an example would be if you have a plain viewController
called myVC
, which has simply a blank white UIView
as its view, you would say this:
CGRect customViewsFrame = CGRectMake(10, 30, 5, 2);
MyCustomView *myView = [[MyCustomView alloc] initWithFrame:customViewsFrame];
[[myVC view] addSubview:myView];
[myView release]; //Since you called "alloc" on this, you have to release it too
然后它将显示在viewController
的视图中,并占用CGRect
指示的空间.
Then it will show up in the viewController
's view, taking up the space indicated by the CGRect
.
CGRect
的坐标在您要添加的超级视图的本地坐标系中指定位置(如果我没有记错的话).
The CGRect
's coordinates specify a location in the local coordinate system of the superview you are adding to, if I'm not mistaken.