开发者

ios access main window or view

开发者 https://www.devze.com 2023-03-23 07:03 出处:网络
I would like to know if there is a simple way to access the main window or view in IOS. Something similar to:

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

开发者_StackOverflow
[UIScreen mainScreen]

Thank you.


Try :

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

or simply :

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


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

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


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

UIWindow *window = mySubview.window;


Simply clever, works from any class:

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

Note: don't forget to import your appDelegate class

0

精彩评论

暂无评论...
验证码 换一张
取 消