在主窗口上创建新的UIWindow

在我的应用程序中,我想在主UIWindow上创建一个新的UIWindow,我写了如下,但它不起作用。 首先,我创建一个UIWindow作为主窗口,然后使其键和可见,然后创建一个新的UIWindow覆盖,但没有任何反应。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor redColor]; ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; window1.backgroundColor = [UIColor redColor]; window1.windowLevel = UIWindowLevelAlert; [window1 makeKeyAndVisible]; return YES; } 

  UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; window1.backgroundColor = [UIColor redColor]; window1.windowLevel = UIWindowLevelAlert; [window1 makeKeyAndVisible]; 

最后我知道它为什么不起作用,因为window1是一个方法var,它会在执行方法后丢失。 所以我为它声明了一个新的@property

 @property (strong, nonatomic) UIWindow *window2; 

并改变代码

  UIWindow *window2 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 80, 320, 320)]; window2.backgroundColor = [UIColor redColor]; window2.windowLevel = UIWindowLevelAlert; self.window2 = window2; [window2 makeKeyAndVisible]; 

有用!

Xcode 8 + Swift

 class ViewController: UIViewController { var coveringWindow: UIWindow? func coverEverything() { coveringWindow = UIWindow(frame: (view.window?.frame)!) if let coveringWindow = coveringWindow { coveringWindow.windowLevel = UIWindowLevelAlert + 1 coveringWindow.isHidden = false } } } 

根据文档 ,要接收没有相关坐标值的事件,例如键盘输入,请将其设为key而不是仅仅! isHidden

 coveringWindow.makeKeyAndVisible() 

您甚至可以控制其背景的透明度,以获得烟雾效果:

 coveringWindow.backgroundColor = UIColor(white: 0, alpha: 0.5) 

请注意,此类窗口需要处理方向更改。

您的window1对象是一个局部变量,当代码用完此方法时,该对象不再存在。 我们创建的任何UIWindow对象都将添加到[[UIApplication sharedApplication] windows] ,但是这个数组只保留对任何UIWindow对象的一周引用,因此由您自己的代码来保持窗口对象存在。为什么苹果实现了这样的我猜,只要应用程序运行, [UIApplication sharedApplication]对象就会存在,这样做是为了避免保留UIWindow对象,这些对象只需要存在一段时间才能永久存在于内存中。

更重要的是,您的代码可以与MRC一起运行。

尝试在mainWindow上添加一个UIView而不是另一个UIWindow

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor redColor]; ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; UIView * viewAlert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; viewAlert.backgroundColor = [UIColor redColor]; [self.window.rootViewController.view addSubView:viewAlert]; /* or you can use following.. [self.window addSubView:viewAlert]; */ [viewAlert release]; //FOR NON ARC return YES; } 

在swift中,可以添加一个新的UIWindow ,如下所示。

 class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var viewController: ViewController? var navigationController: UINavigationController? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.viewController = ViewController(nibName: "ViewController", bundle:NSBundle.mainBundle()) self.navigationController = UINavigationController(rootViewController: self.viewController!) self.window!.rootViewController = self.navigationController // self.window!.addSubview(self.viewController!.view) self.window!.makeKeyAndVisible() return true } //Other methods.. }