繁体   English   中英

将uiview作为子视图添加到主视图中

[英]adding uiview as a subview to the main view

我想添加一个小尺寸的UIView作为现有视图的子视图。

我有一个UIViewController与一个nib名称推送是主视图,我试图创建一个UIView的对象,并在viewcontroller的viewDidLoad方法中使用以下代码分配它

UIView *view = [[UIView alloc] init];

但是,如果我强行键入并运行,那么这个想法并不是建议在UIView上使用alloc并抛出错误

这个你能帮我吗

为上面的答案添加一些东西(相当重要);

CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view = [[UIView alloc] initWithFrame:frame];

然后,你想要实际将它添加到superview(假设视图是self.view)

[self.view addSubview:view];

需要使用框架分配和初始化UIView:

CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view = [[UIView alloc] initWithFrame:frame];
UIView *view = [[UIView alloc] init];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM