低alpha的视图 - 高alpha的子视图

时间:2012-03-06 21:49:09

标签: objective-c ios uiview alpha

我有UIView,其alpha值为.5 我添加了一个alpha值为1的子视图。

子视图似乎继承了父级的alpha值。有没有办法让子视图比其父视图更不透明?

代码如下所示:

CGRect promptFrame = CGRectMake(55, 80, 180, 50);
UIView *inputPrompt = [[UIView alloc] initWithFrame: promptFrame];
[inputPrompt setBackgroundColor: [UIColor darkGrayColor]];
[inputPrompt setAlpha: .5];
inputPrompt.layer.cornerRadius = 8;
inputPrompt.layer.masksToBounds = YES;

CGRect fileTextFieldFrame = CGRectMake(10, 15, 150, 25);
UITextField *filePrompt = [[UITextField alloc] initWithFrame: fileTextFieldFrame];
[filePrompt setBorderStyle:UITextBorderStyleRoundedRect];
[filePrompt setClearButtonMode:UITextFieldViewModeWhileEditing];
[filePrompt setBackgroundColor: [UIColor whiteColor]];
[filePrompt setAlpha: 1];

结果如下: enter image description here

我希望能够看到灰色UIView下面的按钮,但不能看到白色UITextField下面的按钮。我该怎么做?

1 个答案:

答案 0 :(得分:79)

直接设置inputPrompt的背景颜色的alpha而不是其alpha。

[inputPrompt setBackgroundColor:[[UIColor darkGrayColor] colorWithAlphaComponent:0.5]];
//[inputPrompt setAlpha: .5];