【问题标题】:View with low alpha - Subview with high alpha低 alpha 视图 - 高 alpha 子视图
【发布时间】:2012-03-24 10:46:45
【问题描述】:

我有一个UIView,alpha 为 0.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];

结果如下所示:

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

【问题讨论】:

    标签: objective-c ios uiview alpha


    【解决方案1】:

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

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

    【讨论】:

    • 这可以节省我的时间!不敢相信就这么简单。
    • 有没有办法在 XIB 而不是 CODE 中实现这一点?
    猜你喜欢
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2018-03-24
    相关资源
    最近更新 更多