且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iPhone应用程序的UI中添加自定义视图?

更新时间: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.

查看全文