Create alpha UIView with UIlabel in front of it

I would like to make a UIView with alpha and with label.

enter image description here

but I want UILabel in front of everything like this:

enter image description here

How to do it?

Here is the code:

- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 20)]; lbl.text = @"AAAAAA"; lbl.backgroundColor = [UIColor clearColor]; UIView *v = [[UIView alloc] initWithFrame:CGRectMake(50, 210, 230, 50)]; v.backgroundColor = [UIColor greenColor]; v.alpha = 0.5; [v addSubview:lbl]; [self.view addSubview:v]; } 

A green view with alpha 0.5 ... as text, and this is wrong !!!

thanks.

+4
source share
3 answers

Instead of setting the alpha of the whole view, just set the background color to a color with transparency.

So change this:

 v.backgroundColor = [UIColor greenColor]; v.alpha = 0.5; 

For this:

 v.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.5]; 
+13
source

If you want to use inline colors, see colorWithAlphaComponent:

 accessoryView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4]; 
+2
source

In Xcode 4 Interface Builder, you can achieve this by clicking the Background property for the view, clicking "Other" and choosing the colors and opacity values ​​in the color divider that appears.

0
source

Source: https://habr.com/ru/post/1342834/


All Articles