1

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];
RAGHUNATH
  • 187
  • 3
  • 13
  • 1
    Possible duplicate of [iOS - adding/removing a subview programmatically](http://stackoverflow.com/questions/7373544/ios-adding-removing-a-subview-programmatically) – l'L'l Dec 17 '15 at 12:47

2 Answers2

2
UIView *UnitsView = [[UIView alloc]initwithframe: CGRectMake(0,0,width,height)];
[MainView addsubView:UnitsView];
[self.view addSubView:MainView];
vaibby
  • 1,255
  • 1
  • 9
  • 23
1

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];
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41