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; // 先獲取彈出視圖的視圖控制器,再修改隱藏屬性


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM