Gani
Gani

Reputation: 709

adding uiview as a subview to the main view

i want to add an UIView for small size as a subview to existing view.

i have a UIViewController pushed with a nib name which is the main view, i am trying to create an object of UIView and alloc it with dimension with the following code in the viewcontroller's viewDidLoad methoad

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

but the idea is not suggesting the alloc on UIView and throwing an error if i forcefully type and run

please help me out with this

Upvotes: 5

Views: 26929

Answers (3)

Jake
Jake

Reputation: 3973

To add something (rather important) to the answers above;

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

Then, you want to actually add it to the superview (assuming the view is self.view)

[self.view addSubview:view];

Upvotes: 11

zaph
zaph

Reputation: 112857

UIView needs to be alloc'ed and init'ed with a frame:

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

Upvotes: 3

CVertex
CVertex

Reputation: 18237

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

Upvotes: 0

Related Questions