如何以编程方式在iOS中添加SubView?

时间:2015-12-17 12:35:25

标签: ios subview

最初,我创建了一个名为MainView的视图。在View中我放置了一个名为UnitsView.My的视图问题是:如果我想在MainView上添加UnitsView意味着,我是否必须使用self.view或MainView。

UIView *MainView = [[UIViewalloc]initwithframe: CGRectMake    (0,0,self.view.frame.size.width, self.view.frame.size.height) ];
TopView.backgroundColor = [UIColor grayColor];
[self.view addSubview:TopView];

UIView *UnitsView = [[UIView alloc]init];
[self.view (or)MainView addsubView:UnitsView];

2 个答案:

答案 0 :(得分:2)

UIView *UnitsView = [[UIView alloc]initwithframe: CGRectMake(0,0,width,height)];
[MainView addsubView:UnitsView];
[self.view addSubView:MainView];

答案 1 :(得分:0)

要在视图上方添加视图: -

UIView *UnitsView = [[UIView alloc]initwithframe:CGRectMake(0,0,100,100)];
UnitsView.backgroundColor = [UIColor grayColor];
[self.view addSubView:UnitsView];


UIView *SubUnit = [[UIView alloc]initwithframe:CGRectMake(0,0,50,50)];
SubUnit.backgroundColor = [UIColor greenColor];
[self.UnitsView addSubView:SubUnit];