第一种方案:
今天接到项目需求,要求界面是透明的UIView上的视图是透明的,但不影响子视图上的视图。
UIView *blackV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HIGHT)]; blackV.backgroundColor = [UIColor colorWithRed:122/255.0 green:123/255.0 blue:234/255.0 alpha:0.7]; [self.view addSubView: blackV]
第二种方案:
在iOS8后,苹果开放了不少创建特效的接口,其中包括创建毛玻璃的接口。
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur]; effectview.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_WIDTH + 30); effectview.contentView.backgroundColor = [UIColor greenColor]; if (iOS8) { [self.view addSubview:self.effectview]; self.backgroundColor = RGB_COLOR(80, 80, 80); self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"princess"]]; }else{ self.backgroundColor = RGBA_COLOR(20, 20, 20, .7); self.backgroundColor = [UIColor whiteColor]; } [self.view addSubView:effectview];
这段代码是在当前视图控制器上添加了一个UIImageView作为背景图,另外,尽量避免将UIVisualEffectView对象的alpha值设置为小于1.0的值,因为创建半透明的视图会导致系统在离屏渲染时去对UIVisualEffectView对象及所有的相关的子视图做混合操作。这不但消耗CPU/GPU,也可能会导致许多效果显示不正确或者根本不显示。
第三种方案:
UIViewController 推出另外一个半透明的UIViewController
这里通过UIView 也是可以实现的,只是代码已经写好,就尝试了下推出半透明UIViewController的情况
视图1(ViewController1)中添加以下代码
ViewController2 *lagerPicVC = [[ViewController2 alloc]init]; self.modalPresentationStyle = UIModalPresentationCurrentContext;//关键语句,必须有 [self presentViewController:lagerPicVC animated:YES completion:nil]; [self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.868f]];
视图2(ViewController2)中设置背景透明度