Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create alpha UIView with an UIlabel in front of it

i would like make an UIView, with an alpha and with a label.

enter image description here

but i want the UILabel in front of all like this:

enter image description here

How to do it?

Here 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];

}

The green view is with alpha 0.5... like the text and this is wrong!!!

thanks.

like image 802
elp Avatar asked Dec 07 '22 22:12

elp


1 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;

To this:

v.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.5];
like image 122
theChrisKent Avatar answered Dec 25 '22 08:12

theChrisKent