最近看了几个alertView的例子,基本都是keyWindow上addSubView,无意间发现一个PXAlertView的控件采用了自己创建新window弹出alert的方法
if (!_alertWindow) { _alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; _alertWindow.windowLevel = UIWindowLevelAlert; _alertWindow.backgroundColor = [UIColor clearColor]; }
源码:
- (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模拟器截图