vincent
vincent

Reputation: 11

How to make subviews to maintain a non-transparent

I get a trouble in the sense of that:

I create a UIView and set the alpha value to 0.5, and then add a subview with a alpha value in 1, when i run the app, the subview also become transparent, how to keep the subview in non-transparent?

thanks!

Upvotes: 1

Views: 540

Answers (1)

metatation
metatation

Reputation: 167

As you've noticed, setting the alpha on the parent view also affects all subviews. What I do is leave the alpha alone and use a background color with alpha for the parent view and an opaque background color for the child.

For example, in some random view controller:

UIView *transparentView = [[UIView alloc] initWithFrame:self.view.bounds];
transparentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

UIView *opaqueView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
opaqueView.backgroundColor = [UIColor whiteColor];
[transparentView addSubview:opaqueView];
[self.view addSubview:transparentView];

Upvotes: 5

Related Questions