RAGHUNATH
RAGHUNATH

Reputation: 187

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

Upvotes: 1

Views: 4045

Answers (2)

Bhavin Ramani
Bhavin Ramani

Reputation: 3219

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

Upvotes: 1

vaibby
vaibby

Reputation: 1265

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

Upvotes: 2

Related Questions