低 alpha 视图 - 高 alpha 子视图 [英] View with low alpha - Subview with high alpha

查看:43
本文介绍了低 alpha 视图 - 高 alpha 子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView 的 alpha 为 0.5我添加了一个 alpha 为 1 的子视图.

I have a UIView with an alpha of .5 I have added a subview with an alpha of 1.

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

The subview seems to inherit the alpha value of the parent. Is there a way to make the subview more opaque than its parent view?

代码如下:

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

结果如下:

我希望能够看到灰色 UIView 下方的按钮,而不是白色 UITextField 下方的按钮.我该怎么做?

I would like to be able to see the button below the gray UIView but not below the white UITextField. How do I do this?

推荐答案

直接设置 inputPrompt 的背景颜色的 alpha 而不是它的 alpha.

Set the inputPrompt's background color's alpha not its alpha directly.

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

这篇关于低 alpha 视图 - 高 alpha 子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆