How to remove a UIWindow?

让人想犯罪 __ 提交于 2019-12-20 08:57:20

问题


I thought it was easy as [myWindow resignKeyWindow] and [self.window makeKeyAndVisible] but I guess not… Would you guys know what to do?

Thanks :)


回答1:


Do not invoke -resignKeyWindow directly, it was meant to be overridden to execute some code when your UIWindows gets removed. In order to remove old window you need to create new instance of UIWindow and make it -makeKeyAndVisible, the old window will resign its key status. In iOS 4 it will even garbage collect your old UIWindow, provided you don't have any references to it. Doing this in iOS 3.x would have disastrous effects. Warned ya.




回答2:


The correct way to hide a window is to set the hidden property to YES. To remove it from UIApplication's windows property you just release the window (in ARC you set all references to nil).

Of course you would want to have another window in place at this time.




回答3:


If you have any window other than app window, use it ..

let mainWindow = UIApplication.shared.delegate?.window
mainWindow??.makeKeyAndVisible()



回答4:


You cannot remove the window from the app delegate. However you can remove any custom windows created.

To remove the window you must first provide a replacement. So we get the default window.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

We now have access to the default window via the app delegate's window property.

Now get the original or custom navigation controller. Assign self to rootViewController.

Calling makeKeyandVisible removes any windows and assigns the app delegate's window as the key window. Set rootViewController to the navigation controller you just created and you are good to go!

DEMONavigationController *demoNav = [[DEMONavigationController alloc]initWithRootViewController:self];
[appDelegate.window makeKeyAndVisible];
appDelegate.window.rootViewController = demoNav;



回答5:


I have the same issue, it may helps.

You needs to destroy all strong ref before remove and dealloc a windows, especially the rootWindowController. I think below code is enough to delete any window:

[self.window.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
self.window.rootViewController = nil;
[self.window resignKeyWindow];
[self.window removeFromSuperview];


来源:https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!