ios ios访问主窗口或视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6820017/
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 20:51:39  来源:igfitidea点击:

ios access main window or view

iosuiviewwindowmain

提问by user346443

I would like to know if there is a simple way to access the main window or view in IOS. Something similar to:

我想知道在IOS中是否有一种简单的方法可以访问主窗口或视图。类似于:

[UIScreen mainScreen]

Thank you.

谢谢你。

回答by

Try :

尝试 :

UIWindow *frontWindow = [[[UIApplication sharedApplication] windows]
                                                            lastObject];

or simply :

或者干脆:

UIWindow *frontWindow = [[UIApplication sharedApplication] keyWindow];

回答by diachedelic

The main window is best found using UIWindow * mainWindow = [UIApplication sharedApplication].windows.firstObject;

最好使用主窗口找到 UIWindow * mainWindow = [UIApplication sharedApplication].windows.firstObject;

Using keyWindowcan return a keyboard or UIAlertView window, which lie above your application window.

使用keyWindow可以返回一个键盘或 UIAlertView 窗口,它位于您的应用程序窗口上方。

回答by Caleb

If you have a pointer to some subview in the window, you can easily access its window:

如果您有指向窗口中某个子视图的指针,则可以轻松访问其窗口:

UIWindow *window = mySubview.window;

回答by Resty

Simply clever, works from any class:

非常聪明,适用于任何课程:

[(YourAppDelegate *)[[UIApplication sharedApplication] delegate] window];

Note: don't forget to import your appDelegate class

注意:不要忘记导入您的 appDelegate 类