ios 如何删除一个 UIWindow?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4544489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:23:18  来源:igfitidea点击:

How to remove a UIWindow?

iphonecocoa-touchiosios4uiwindow

提问by G33kz0r

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

我认为这很容易[myWindow resignKeyWindow][self.window makeKeyAndVisible]但我想不是……你们知道该怎么做吗?

Thanks :)

谢谢 :)

采纳答案by bioffe

Do not invoke -resignKeyWindowdirectly, 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.

不要-resignKeyWindow直接调用,当你的 UIWindows 被删除时,它应该被覆盖以执行一些代码。为了删除旧窗口,您需要创建 UIWindow 的新实例并创建它-makeKeyAndVisible,旧窗口将放弃其关键状态。在 iOS 4 中,它甚至会垃圾收集您的旧UIWindow,前提是您没有任何对它的引用。在 iOS 3.x 中这样做会产生灾难性的影响。警告过你。

回答by Nikolai Ruhe

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

隐藏窗口的正确方法是将hidden属性设置为 YES。要从 UIApplication 的windows属性中删除它,您只需释放窗口(在 ARC 中,您将所有引用设置为 nil)。

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

当然,此时您会希望有另一个窗口。

回答by Vadim Bulavin

Here is how you can remove UIWindowon iOS 13 in a backward-compatible way. Tested on iOS 12, iOS 13, iPadOS with multi-window support:

以下是如何UIWindow以向后兼容的方式在 iOS 13 上删除。在支持多窗口的 iOS 12、iOS 13、iPadOS 上测试:

extension UIWindow {
    func dismiss() {
        isHidden = true

        if #available(iOS 13, *) {
            windowScene = nil
        }
    }
}

Usage:

用法:

// Detect key window
let keyWindow = UIApplication.shared.windows.first { 
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
.isKeyWindow } // Dismiss key window (if any) keyWindow?.dismiss()

回答by Dipesh Somvanshi

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.

要拆除窗户,您必须首先提供更换件。所以我们得到了默认窗口。

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

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

我们现在可以通过应用程序委托的window属性访问默认窗口。

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

现在获取原始或自定义导航控制器。将 self 分配给rootViewController.

Calling makeKeyandVisibleremoves 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!

调用makeKeyandVisible会删除任何窗口并将应用程序委托的窗口指定为关键窗口。将 rootViewController 设置为您刚刚创建的导航控制器,您就可以开始了!

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

回答by Avinash

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

如果您有应用程序窗口以外的任何窗口,请使用它..

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

回答by flypig

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:

您需要在删除和释放窗口之前销毁所有强引用,尤其是 rootWindowController。我认为下面的代码足以删除任何窗口:

##代码##