视图的alpha属性可能会影响子视图的透明度

本篇博客要说一下alpha这个属性。

alpha属性是用来设置视图的透明度的,可如果直接使用alpha属性,并且使用此属性的视图有子视图,那么其子视图的透明度也会跟着受影响。先看代码以及效果图吧:

代码:

 UIView * redV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    redV.backgroundColor = [UIColor redColor];

    redV.alpha = 0.5;

    [self.view addSubview:redV];

    

    UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    lbl.text = @"我";

    lbl.textColor = [UIColor whiteColor];

    lbl.textAlignment = NSTextAlignmentCenter;

    lbl.backgroundColor = [UIColor blackColor];

    [redV addSubview:lbl];


效果图:


以上效果图是直接使用alpha属性的效果。


代码:

UIView * redV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    redV.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];

//    redV.alpha = 0.5;

    [self.view addSubview:redV];

    

    UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    lbl.text = @"我";

    lbl.textColor = [UIColor whiteColor];

    lbl.textAlignment = NSTextAlignmentCenter;

    lbl.backgroundColor = [UIColor blackColor];

    [redV addSubview:lbl];


效果图:



看客根据所需要的效果选择相应的代码吧!


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值