iOS设置父view的透明度与子view的不一样

前言:

在开发中我们经常需要根据需求更改控件透明度,一般会采用修改view的alpha来实现,例如:

UIView *blackV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HIGHT)];  
blackV.backgroundColor = [UIColor blackColor];  
blackV.alpha = 0.7; 

这样就修改了blackV的颜色为黑色、透明度为0.7。

但是有时候我们需要让父控件有一定的透明度,而在父控件之上的子控件为不透明。这时如果我们只是单纯将子控件加入父控件,就算设置子控件的alpha为1,子控件的透明度还是会跟父控件一致,例如下面:

self.view.backgroundColor = [UIColor blueColor];

UIView *parentV = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
parentV.backgroundColor = [UIColor redColor];
parentV.alpha = 0.7;
[self.view addSubview: parentV];

UIView *childV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
childV.backgroundColor = [UIColor yellowColor];
childV.alpha = 1;
[parentV addSubview: childV];

显示效果为:

这里写图片描述

可以看到黄色的子控件背面仍可看到蓝色背景。

解决方案:

1、只设置黑白背景色
如果要求背景颜色只为黑白背景时可以采用如下方法:

[UIColor colorWithWhite:0.f alpha:0.7];  

其中第一个参数是指定背景颜色,从0~1是由黑到白,第二个参数是指定透明度。

例如:

self.view.backgroundColor = [UIColor blueColor];

    UIView *parentV = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    parentV.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5];
    [self.view addSubview: parentV];

    UIView *childV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    childV.backgroundColor = [UIColor yellowColor];
    childV.alpha = 1;
    [parentV addSubview: childV];

显示结果为:

这里写图片描述

可以看到父控件的背景色为黑色,透明度为0.5;子控件的黄色背景透明度为1,并没有跟随父控件的透明度变化。

2、设置任意颜色的背景色

上边的方法只能设置单一背景颜色时的透明度,有时我们需要设置背景颜色为更多色彩,这时可以用下边方法来实现:

[UIColor colorWithRed:122/255.0 green:123/255.0 blue:234/255.0 alpha:0.7]

与第一种方法类似,只不过将第一个颜色参数改为了rgb颜色,这样就可以设置多种颜色了。
例如:

self.view.backgroundColor = [UIColor blueColor];

    UIView *parentV = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    parentV.backgroundColor = [UIColor colorWithRed:186/255.0 green:85/255.0 blue:211/255.0 alpha:0.7];
    [self.view addSubview: parentV];

    UIView *childV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    childV.backgroundColor = [UIColor yellowColor];
    childV.alpha = 1;
    [parentV addSubview: childV];

显示结果为:
这里写图片描述

子控件的透明度为1,并没有跟随父控件的透明度变化。

3、采用 colorWithAlphaComponent

这一种方法也是本人比较常用的,使用起来也比较方便:

[[UIColor redColor] colorWithAlphaComponent:0.7];  

这种方法可以任意指定父view的颜色,透明度。
例如:

 UIView *parentV = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    parentV.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
    [self.view addSubview: parentV];

    UIView *childV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    childV.backgroundColor = [UIColor yellowColor];
    childV.alpha = 1;
    [parentV addSubview: childV];

显示结果为:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值