View with low alpha - Subview with high alpha

安稳与你 提交于 2019-11-30 12:36:53

问题


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

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?

code looks like this:

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

The result looks like this:

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?


回答1:


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

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


来源:https://stackoverflow.com/questions/9592470/view-with-low-alpha-subview-with-high-alpha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!