UIWindow & UIWindowLevel的应用引导

原创 2013年12月02日 17:09:49

最近看了几个alertView的例子,基本都是keyWindow上addSubView,无意间发现一个PXAlertView的控件采用了自己创建新window弹出alert的方法

        if (!_alertWindow) {
            _alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
            _alertWindow.windowLevel = UIWindowLevelAlert;
            _alertWindow.backgroundColor = [UIColor clearColor];
        }

看到了windowLever这个属性,在网上查到很多《UIWindow & UIWindowLevel详解》和《UIWindow & UIWindowLevel笔记》,让我迷惑了,实际运行结果和文章的描述完全不同了。

源码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor yellowColor];
    [self.window makeKeyAndVisible];
    
    UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    normalWindow.backgroundColor = [UIColor blueColor];
    normalWindow.windowLevel = UIWindowLevelNormal;
    [normalWindow makeKeyAndVisible];
    
    CGRect windowRect = CGRectMake(50, 
                                   50, 
                                   [[UIScreen mainScreen] bounds].size.width - 100, 
                                   [[UIScreen mainScreen] bounds].size.height - 100);
    UIWindow *alertLevelWindow = [[UIWindow alloc] initWithFrame:windowRect];
    alertLevelWindow.windowLevel = UIWindowLevelAlert;
    alertLevelWindow.backgroundColor = [UIColor redColor];
    [alertLevelWindow makeKeyAndVisible];
    
    UIWindow *statusLevelWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 50, 320, 20)];
    statusLevelWindow.windowLevel = UIWindowLevelStatusBar;
    statusLevelWindow.backgroundColor = [UIColor blackColor];
    [statusLevelWindow makeKeyAndVisible];
    
    NSLog(@"Normal window level: %f", UIWindowLevelNormal);
    NSLog(@"Normal window level: %f", UIWindowLevelAlert);
    NSLog(@"Normal window level: %f", UIWindowLevelStatusBar);
    
    return YES;
}

运行结果,图一:



而我的运行结果,图二



蓝颜色出来了

如果改成:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor yellowColor];
    
    UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    normalWindow.backgroundColor = [UIColor blueColor];
    normalWindow.windowLevel = UIWindowLevelNormal;
    [normalWindow makeKeyAndVisible];

    [self.window makeKeyAndVisible];


[self.windowmakeKeyAndVisible];移动到最后

又和图一样了,所以makeKeyAndVisible还是根据先后顺序显示window的


另外,代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    self.viewController.view.backgroundColor = [UIColor yellowColor];
    [self.window makeKeyAndVisible];
    
    UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    normalWindow.backgroundColor = [UIColor blueColor];
    normalWindow.windowLevel = UIWindowLevelNormal;
    [normalWindow makeKeyAndVisible];
    
    CGRect windowRect1 = CGRectMake(150,
                                    150,
                                    [[UIScreen mainScreen] bounds].size.width - 250,
                                    [[UIScreen mainScreen] bounds].size.height - 400);
    UIWindow *alertLevelWindow1 = [[UIWindow alloc] initWithFrame:windowRect1];
    alertLevelWindow1.windowLevel = UIWindowLevelAlert+1;
    alertLevelWindow1.backgroundColor = [UIColor purpleColor];
    [alertLevelWindow1 makeKeyAndVisible];
    
    CGRect windowRect = CGRectMake(50,
                                   50,
                                   [[UIScreen mainScreen] bounds].size.width - 100,
                                   [[UIScreen mainScreen] bounds].size.height - 100);
    UIWindow *alertLevelWindow = [[UIWindow alloc] initWithFrame:windowRect];
    alertLevelWindow.windowLevel = UIWindowLevelAlert;
    alertLevelWindow.backgroundColor = [UIColor redColor];
    [alertLevelWindow makeKeyAndVisible];
    
    UIWindow *statusLevelWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 50, 320, 20)];
    statusLevelWindow.windowLevel = UIWindowLevelStatusBar;
    statusLevelWindow.backgroundColor = [UIColor blackColor];
    [statusLevelWindow makeKeyAndVisible];
    
    NSLog(@"Normal window level: %f", UIWindowLevelNormal);
    NSLog(@"Alert window level: %f", UIWindowLevelAlert);
    NSLog(@"StatusBar window level: %f", UIWindowLevelStatusBar);
    
    return YES;
}

结果如下图三:



紫色的部分是UIWindowLevelAlert+1,虽然在前面调用的makeKeyAndVisible,但还是显示在最上面了

windowLevel是可以自己任意设置的,原理上只要比2000(UIWindowLevelAlert)大,应该就可以浮在最上面了,我没有测试


我的测试环境是xcode4.5.2,IOS6模拟器截图



相关文章推荐

UIWindow & UIWindowLevel详解

一、UIWindow是一种特殊的UIView,通常在一个程序中只会有一个UIWindow,但可以手动创建多个UIWindow,同时加到程序里面。UIWindow在程序中主要起到三个作用:   1...

手动创建UIWindow及UIWindowLevel的说明

手动创建UIWindow及UIWindowLevel的说明和实例

iOS SDK详解之UIWindow(让视图在最上层)

这个iOS SDK详解的专栏地址 http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html 前言: 很多时候,我们希望视图显...

【读书笔记】iOS-UIWindow-WindowLevel

WindowLevel是UIWindow的一个属性。系统定义的一共有3种。 UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal; UIKIT_E...

iOS 如何获取应用当前的控制器 UIWindow & UIWindowLevel笔记

-:有时候我们需要在非控制器类中用到应用当前的控制器,比如在工具类中弹窗、跳转界面等等。下面是获取应用当前控制器的代码: - (UIViewController *)getCurrentVC { ...

UIWindow 和 UIWindowLevel 的理解

一、 UIWindow是一种特殊的UIView,通常在一个程序中只有一个UIWindow,但是可以手动创建多个UIWindow,同时添加到程序里面。UIWindow在程序中主要的作用有:   1、作...

UIWindow & UIWindowLevel笔记

本文转载自:http://www.cnblogs.com/smileEvday/archive/2012/03/27/2420362.html 一、UIWindow是一种特殊的UIView,...

UIWindow & UIWindowLevel

一、UIWindow是一种特殊的UIView,通常在一个程序中只会有一个UIWindow,但可以手动创建多个UIWindow,同时加到程序里面。UIWindow在程序中主要起到三个作用:  1、作为容...
  • MrEshen
  • MrEshen
  • 2015年05月01日 21:22
  • 137

ios-day11-01(UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口)

UIWindow的常用方法: - (void)makeKeyWindow; 让当前UIWindow变成keyWindow(主窗口) - (void)makeKeyAndVisible...

ios-day11-01(UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口)

UIWindow的常用方法: - (void)makeKeyWindow; 让当前UIWindow变成keyWindow(主窗口) - (void)makeKey...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UIWindow & UIWindowLevel的应用引导
举报原因:
原因补充:

(最多只允许输入30个字)