美文网首页
rootViewController问题

rootViewController问题

作者: 张俊凯 | 来源:发表于2018-08-16 18:04 被阅读132次

在app中,想拿到rootViewController,一般用的方法是window.rootViewController,获取到window有两种方法,第一种

UIWindow *window = [UIApplication sharedApplication].keyWindow;

第二种

AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIWindow *window =  appdelegate.window;

那么,从第二种方法拿到的window的根控制器通常是自己设置的那款控制器,第一种则不一样,在app从后台回到前台的时候有可能是

UIApplicationRotationFollowingController

也就是说在app旋转的时候,keyWindow不是appdelegate的window,是系统自定义的window,自己还可以自定义window,显示哪个,哪个就是keywindow。
(完)
参考链接:https://www.jianshu.com/p/ae84cd31d8f0

  • 补充:想要在最底部显示一个蒙版控制器视图,可以用根控制器添加蒙版控制器,再用根控制器的视图添加蒙版视图,这样就会形成一个视图层级
UIViewController *rootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    
//还可以判断一下跟控制器有没有弹出其他控制器 找到最靠前,正在显示的控制器,用while方法一层一层往下找
while (rootVC.presentedViewController != nil) {
        rootVC = rootVC.presentedViewController;
    }

[rootVC addChildViewController:sideVC];
    [rootVC.view addSubview:sideVC.view];

//消失的方法,调用“从父控制器移除自己”的方法
[self removeFromParentViewController];

参考链接:https://github.com/djardon/SideMenuFrostedAnimated

相关文章

网友评论

      本文标题:rootViewController问题

      本文链接:https://www.haomeiwen.com/subject/ovgybftx.html