简体   繁体   中英

How to add SubView in iOS programmatically?

Intially,I created view called MainView.Inside that View I have place the view called UnitsView.My Problem is: If I want to add the UnitsView over MainView means, Whether I have to use self.view or 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];
UIView *UnitsView = [[UIView alloc]initwithframe: CGRectMake(0,0,width,height)];
[MainView addsubView:UnitsView];
[self.view addSubView:MainView];

To add view above view:-

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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