iOS - iOS的Window控制(显示/隐藏)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS - iOS的Window控制(显示/隐藏)相关的知识,希望对你有一定的参考价值。

参考技术A 我这里因为工程里已经集成了Aspects所以就用hook的方式

UIActionSheet

UIAlertView

为了使用方便,就直接给window定一个分类

==这里用NSHashTable 防止重复储存==

iOS 获取当前界面所在的视图控制器

当前界面所在的视图控制器 逐步完善的过程

一、

//获取当前屏幕显示的viewcontroller   (这里面获取的相当于rootViewController)
- (UIViewController *)getCurrentVC  
{  
    UIViewController *result = nil;  
      
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];  
    if (window.windowLevel != UIWindowLevelNormal)  
    {  
        NSArray *windows = [[UIApplication sharedApplication] windows];  
        for(UIWindow * tmpWin in windows)  
        {  
            if (tmpWin.windowLevel == UIWindowLevelNormal)  
            {  
                window = tmpWin;  
                break;  
            }  
        }  
    }  
      
    UIView *frontView = [[window subviews] objectAtIndex:0];  
    id nextResponder = [frontView nextResponder];  
      
    if ([nextResponder isKindOfClass:[UIViewController class]])  
        result = nextResponder;  
    else  
        result = window.rootViewController;  
      
    return result;  
}  

 

二、不够完善

//获取到当前所在的视图
+ (UIViewController *)presentingVC{
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows){
            if (tmpWin.windowLevel == UIWindowLevelNormal){
                window = tmpWin;
                break;
            }
        }
    }
    UIViewController *result = window.rootViewController;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[MYTabBarViewController class]]) {
        result = [(MYTabBarViewController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController *)result topViewController];
    }
    return result;
}

 

三、参数传入导航试图控制器或者UITabBarController self.window.rootViewController 也可。(这个比较通用)

+ (UIViewController *)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}

 

当然也可以根据自己项目的框架和业务逻辑 自己写相应的方法!

 

以上是关于iOS - iOS的Window控制(显示/隐藏)的主要内容,如果未能解决你的问题,请参考以下文章

ios:更改 window.rootViewController 和内存管理

当presentviewcontroller ios时标签栏控制器显示黑屏

Xcode - iOS - 要更改的导航控制器场景顺序

iOS 获取当前界面所在的视图控制器

iOS 获取当前界面所在的视图控制器

Swift ios 检查存在哪个视图控制器