iOS模态弹出半透明视图控制器

项目中需要实现点击按钮出现的视图全屏覆盖,呈半透明状态可以看到下面的视图?

解决方案:

绕了很多弯路原来可以使用模态弹出一个视图控制器

在iOS8之后只需要设置一个最新的属性

SecondViewController *vc=[[SecondViewController alloc]init];
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:vc animated:NO completion:^{
//        vc.view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        vc.view.backgroundColor = [UIColor orangeColor];
        vc.view.alpha = 0.5;
    }];

在iOS7或更低需要设置你的window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext

AppDelegate *appdelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    UIViewController *vc=[[UIViewController alloc]init];
    appdelegate.window.rootViewController.modalPresentationStyle=UIModalPresentationCurrentContext;
    [appdelegate.window.rootViewController presentViewController:vc animated:YES completion:^{
        vc.view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        appdelegate.window.rootViewController.modalPresentationStyle=UIModalPresentUIModalPresentationFullScreen;
    }];

 实现完成后发现了一个bug, 如果当presentingVC有根视图控制器tabBarController,上面的设置会使tabBar未被覆盖,意思就好像是你有一直看到presentingVC直接导致不会走viewWiillAppear,不能在原视图即将出现时把隐藏tabBar的属性改回来。此时

在present的时候敲一行

    self.tabBarController.tabBar.hidden = YES; // 隐藏tabBar

在dismiss的时候敲一行

    self.presentingViewController.tabBarController.tabBar.hidden = NO; // 先获取弹出视图的视图控制器,再修改隐藏属性

转载于:https://www.cnblogs.com/xs514521/p/5643835.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值