如題,開發(fā)中經(jīng)常會碰到需要背景透明,上面顯示的控件不透明的情況胳挎,一般首先都會想到先父子視圖布局,然后設(shè)置需要透明度的父視圖的alpha值溺森,但是慕爬,如果直接設(shè)置該視圖的alpha值,會導(dǎo)致該視圖上的所有子控件也具有相應(yīng)的透明度屏积。目前我用6種方法進(jìn)行解決澡罚,其中前四種的核心都是通過設(shè)置父視圖的背景顏色來達(dá)到目的。
1肾请、如果是xib進(jìn)行UI布局,那么在父視圖上對背景進(jìn)行設(shè)置:
2更胖、使用colorWithWhite:alpha:方法
0表示黑色铛铁,1表示白色,可取0~1中間的值却妨。這種方法只能設(shè)置黑白之間的透明度
3饵逐、使用colorWithRed:green:blue:alpha:方法
這種方法可以設(shè)置任何我們需要的顏色
4、使用colorWithAlphaComponent:方法
這個方法是返回一個具有透明度的UIColor彪标,再將這個UIColor賦值給相應(yīng)的view
5倍权、父子視圖平級
即子視圖也直接添加在self.view上,這樣子視圖不是添加在父視圖上面捞烟,父視圖上設(shè)置相應(yīng)的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];