UIWindow & UIWindowLevel的应用引导

最近看了几个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;
}

运行结果,图一:

UIWindow & UIWindowLevel的应用引导_第1张图片


而我的运行结果,图二

UIWindow & UIWindowLevel的应用引导_第2张图片


蓝颜色出来了

如果改成:

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的应用引导)