ios知识

iOS设置透明视图(父视图)上显示不透明(子视图)视图

2019-01-30  本文已影响0人  叫我小小诗

如题,开发中经常会碰到需要背景透明,上面显示的控件不透明的情况,一般首先都会想到先父子视图布局,然后设置需要透明度的父视图的alpha值,但是,如果直接设置该视图的alpha值,会导致该视图上的所有子控件也具有相应的透明度。目前我用6种方法进行解决,其中前四种的核心都是通过设置父视图的背景颜色来达到目的。

效果图: 效果图.png 方法:
1、如果是xib进行UI布局,那么在父视图上对背景进行设置: xib设置透明度.jpg
2、使用colorWithWhite:alpha:方法
0表示黑色,1表示白色,可取0~1中间的值。这种方法只能设置黑白之间的透明度
3、使用colorWithRed:green:blue:alpha:方法
这种方法可以设置任何我们需要的颜色

4、使用colorWithAlphaComponent:方法
这个方法是返回一个具有透明度的UIColor,再将这个UIColor赋值给相应的view
5、父子视图平级
即子视图也直接添加在self.view上,这样子视图不是添加在父视图上面,父视图上设置相应的alpha值就不会影响到子视图
6.使用具有透明度的背景图片。
让UI帮你做一张你需要的图片,直接放上去。
代码如下:

//1------
    UIView * fatherView1 = [[UIView alloc] initWithFrame:CGRectMake(50, 180, Width - 100 , 50)];
    fatherView1.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [self.imageView addSubview:fatherView1];
    
    UILabel * subLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, fatherView1.frame.size.width - 20, 30)];
    subLabel.backgroundColor = [UIColor blueColor];
    subLabel.textColor = [UIColor whiteColor];
    subLabel.text = @"colorWithWhite:alpha:";
    [fatherView1 addSubview:subLabel];
    
    //2-------
    UIView * fatherView2 = [[UIView alloc] initWithFrame:CGRectMake(50, 260, Width - 100, 50)];
    fatherView2.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self.imageView addSubview:fatherView2];
    
    UILabel * subLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, fatherView2.frame.size.width - 20, 30)];
    subLabel2.textColor = [UIColor whiteColor];
    subLabel2.backgroundColor = [UIColor blueColor];
    subLabel2.text = @"colorWithRed:green:blue:alpha:";
    [fatherView2 addSubview:subLabel2];
    
    
    //3-------
    UIView * fatherView3 = [[UIView alloc] initWithFrame:CGRectMake(50, 340, Width - 100, 50)];
    UIColor * fatherColor = [UIColor blackColor];
    fatherView3.backgroundColor = [fatherColor colorWithAlphaComponent:0.5];
    [self.imageView addSubview:fatherView3];
    
    UILabel * subLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, fatherView3.frame.size.width - 20, 30)];
    subLabel3.backgroundColor = [UIColor blueColor];
    subLabel3.textColor = [UIColor whiteColor];
    subLabel3.text = @"colorWithAlphaComponent:";
    [fatherView3 addSubview:subLabel3];
    
    
    //4------
    UIView * fatherView4 = [[UIView alloc] initWithFrame:CGRectMake(50, 420, Width - 100, 50)];
    fatherView4.backgroundColor = [UIColor blackColor];
    fatherView4.alpha = 0.5;
    [self.imageView addSubview:fatherView4];
    
    UILabel * subLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(60, 430, Width - 120, 30)];
    subLabel4.backgroundColor = [UIColor blueColor];
    subLabel4.text = @"父子视图平级,子视图也直接添加在view上";
    subLabel4.textColor = [UIColor whiteColor];
    subLabel4.adjustsFontSizeToFitWidth = YES;
    [self.imageView addSubview:subLabel4];
    
    
    //5-------
    UIImageView * fatherImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 500, Width - 100, 50)];
    fatherImage.image = [UIImage imageNamed:@"bgColorAlpha.png"];
    [self.imageView addSubview:fatherImage];
    
    UILabel * subLabel5 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, fatherImage.frame.size.width - 20, 30)];
    subLabel5.backgroundColor = [UIColor blueColor];
    subLabel5.text = @"半透明背景图";
    subLabel5.textColor = [UIColor whiteColor];
    [fatherImage addSubview:subLabel5];

上一篇 下一篇

猜你喜欢

热点阅读