简体   繁体   中英

Create alpha UIView with an UIlabel in front of it

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

在此输入图像描述

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

在此输入图像描述

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.

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

如果要使用内置颜色,请参阅colorWithAlphaComponent:

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

在XCode 4 Interface Builder中,您可以通过单击视图的Background属性,单击“Other”,然后在显示的颜色选择器中选择颜色和不透明度值来实现此目的。

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