要实现该效果其实很简单,但如果一直纠结把父视图用view.alpha=0.5属性设置父视图的话会很麻烦。
简单介绍两方法:
1:将透明父视图和要显示的子实体添加在同一个父视图上,先添加透明视图,在加子视图,这样看起来就是父视图透明子视图不透明。
```
-(void)createUI{
UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 100, 375, 400)];
backView.backgroundColor=[UIColor grayColor];
backView.alpha=0.5;
[self.view addSubview:backView];
UIView *yellowView=[[UIView alloc]initWithFrame:CGRectMake(50, 150, 275, 300)];
yellowView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:yellowView];
}
2:父视图使用RGB方法设置颜色和透明度
-(void)createUI{
UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 100, 375, 400)];
backView.backgroundColor=[UIColor colorWithRed:140/255.0 green:140/255.0 blue:140/255.0 alpha:0.5];
[self.view addSubview:backView];
UIView *yellowView=[[UIView alloc]initWithFrame:CGRectMake(50, 50, 275, 300)];
yellowView.backgroundColor=[UIColor yellowColor];
[backView addSubview:yellowView];
}
```
上面两种方法都可以非常简单的实现父视图透明子视图不透明