Matoe
Matoe

Reputation: 2758

A new view is not visible

I just made a view using CGRectMake but the view is not visible. Here's my code until now:

mainView = [[UIView alloc] initWithFrame:CGRectMake(0,20,320,460)];
    mainView.userInteractionEnabled = YES;
    mainView.backgroundColor = [UIColor whiteColor];
    mainView.alpha = 1.0;
    [mainView setHidden:NO];

Is there another property I need to add in order to make it be visible/functional?

Upvotes: 0

Views: 88

Answers (3)

Felipe Sabino
Felipe Sabino

Reputation: 18205

You need to add it as a subview in your UIViewController or you UIApplication keyWindow.

Also, you have to make sure its frame position is inside the parentView application bounds IF the parent view has the clipToBounds property set to YES

Upvotes: 1

Deeps
Deeps

Reputation: 4577

You forgot to add view on main view. So do like below.

[self.view addSubview:mainView];

So Just replace whole code with below.

mainView = [[UIView alloc] initWithFrame:CGRectMake(0,20,320,460)];
mainView.userInteractionEnabled = YES;
mainView.backgroundColor = [UIColor whiteColor];
mainView.alpha = 1.0;
[mainView setHidden:NO];
[self.view addSubview:mainView];

Upvotes: 4

odrm
odrm

Reputation: 5249

You have to add the new view to an existing view. You could use (for example):

[[[UIApplication sharedApplication] keyWindow] addSubview:myNewView];

If you are using a UIViewController, it would be:

[[myViewController view] addSubview:myNewView];

Upvotes: 1

Related Questions