ios access main window or view

前端 未结 4 1913
小蘑菇
小蘑菇 2021-02-07 05:12

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

[UIScreen mainScreen]

Thank you.<

相关标签:
4条回答
  • 2021-02-07 05:13

    Simply clever, works from any class:

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

    Note: don't forget to import your appDelegate class

    0 讨论(0)
  • 2021-02-07 05:22

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

    UIWindow *window = mySubview.window;
    
    0 讨论(0)
  • 2021-02-07 05:24

    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.

    0 讨论(0)
  • 2021-02-07 05:30

    Try :

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

    or simply :

    UIWindow *frontWindow = [[UIApplication sharedApplication] keyWindow];
    
    0 讨论(0)
提交回复
热议问题