开发者

Create alpha UIView with an UIlabel in front of it

i would like make an UIView, wit开发者_如何学Goh an alpha and with a label.

Create alpha UIView with an UIlabel in front of it

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

Create alpha UIView with an UIlabel in front of it

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


If you want to use the built in colors, see colorWithAlphaComponent:

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


In the XCode 4 Interface Builder you can achieve this by clicking on the Background property of a view, clicking on "Other", and choosing the color and the opacity values in the color picker that appears.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜