【问题标题】:UIView change background transparencyUIView 改变背景透明度
【发布时间】:2012-12-01 05:09:21
【问题描述】:

我正在开发一个 Iphone 应用程序。

我正在创建一个 UIView:

UIView *popupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];

然后我想用一张图片作为视图的背景:

UIColor * bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_bg.png"]];

popupView.backgroundColor = bgColor;

有没有办法让背景半透明?可能将 alfa 值设置为 0.5 或给它一个不透明度。

我尝试使用 Photoshop 使图像透明,但是当我将其设置为背景时,它不再透明。

请注意,我只需要背景是透明的,而不是子视图

非常感谢您的帮助

【问题讨论】:

  • 如何设置alpha?谢谢
  • 我以为你确实知道如何设置它 :-) Indler 的回答来得很快。

标签: iphone objective-c background transparency


【解决方案1】:

有一个简单的技巧。添加同一帧的子视图并将其更改为 alpha。

UIView *popupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
UIView *bgView = [[UIView alloc] initWithFrame:popupView.frame];
UIColor * bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_bg.png"]];
bgView.backgroundColor = bgColor;
bgView.alpha = 0.5;
[popupView addSubview:bgView];

【讨论】:

  • +1,我在想同样的答案,但收到了通知:(
【解决方案2】:

您可以将 alpha 降低到 0.5 以使其透明或根据需要进行更改。

你可以参考这个LINK

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2016-12-16
    • 2022-01-23
    相关资源
    最近更新 更多