ios access main window or view
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
精彩评论