简体   繁体   中英

Putting UIView with alpha 1 on top of UIView with alpha 0.5

I'm trying to have a modular uiview that will be placed on top of another view. The modular view has an alpha value of 0.5, and appears in the middle of the main view.

Now, I would like for text to be rendered on that modular UIView. However, whenever I:

[modularView addSubview:text]

it looks all hazy.

How can I make the text tack sharp, but keep the alpha of its parent view at 0.5?

Thanks!

Set the alpha for the background color rather than for the whole view:

[modularView setBackgroundColor:[UIColor colorWithRed:0.0 
                                         green:0.0 
                                          blue:0.0 
                                         alpha:0.5]];

This will allow any subviews to retain full opacity. The color in my code is black. Just set the RGB values for whatever color you're wanting.

You could also just use a UILabel instead of a UIView and set it's background the same way. Then you don't need to add a subview--though I'm not sure what else you have in your modularView view.

I'm doing the same on a project i'm working on, my "modalview" is on top of a fullscreen image.

My "modalview" has alpha 1.0 and the background color is black with 50% opacity.

Then the text is white, with default highlight and a dark shadow with 1.0 h and v-offset.

Hope it helps...

PS: make sure you're adding the text in front of the "modalview", not behind it ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM