本篇博客要说一下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];
效果图:
看客根据所需要的效果选择相应的代码吧!