Behind the Scenes heroes UIWindow

Source: Internet
Author: User

Let's look at a snippet of code.

    UIWindow*window = [UIApplication sharedApplication].keyWindow;    self.frame =window.bounds;    [windowaddSubview:self];

It looks amazing, doesn't it? Can actually add view directly to the UIWindow, a bit of dark magic feeling. UIWindow exactly what it is, how to use it, and we look down at the explanations from the official iOS.

Each iOS app must contain a window that displays an interactive page for the app. Although the role of window is critical, in most cases, developers do not need to deal with it. Only when the app supports display to the external screen will we use the window.

Profile:

In iOS, window contains a view and is responsible for managing the view's presence on the device. The screen associated with window represents the display for a particular device. If your app enables users to project a view onto an external display screen, you'll need to create another window to display the content.

Key concepts:

The 1,uiwindow class is a subclass of UIView and can be considered a special uiview.
2, a generic application has only one UIWindow object.
3, even if there are multiple UIWindow objects, only one UIWindow can accept the user's touchscreen event.

The important role of window:

The function 1,window contains the visible area of the application;
The role of 2,window is responsible for receiving touch events and distributing them to various views;
The role of 3,window is responsible for receiving changes in the screen, and then distributing them to each view.

About the creation of window:

1, an application contains at least one window;
2, in an era without storyboard, we had to create a window and associate it with a rootviewcontroller when we entered the application. When connected, window automatically add its view and calculates the appropriate display size. The size of the display is affected by several factors: whether the status bar is visible, the current device's portrait and screen condition, and whether the view is full screen. The code is as follows:

    - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    myViewController = [[MyViewController alloc] init];    window.rootViewController = myViewController;    [window makeKeyAndVisible];    returnYES;    }

3, in the era of storyboard, the main storyboard will automatically create a window, silently for our business.

Create another window The only purpose is to support the display to the external screen:

If your app supports an external display screen, you'll need to create an extra window for temporary content. You can display the same content on the external display screen-called as "Mirror", which is what happens by default, and of course you can show different things.

Some events are only sent to the main window:

Only the main window can receive keyboard events, as well as non-touch events. There can be only one main window at a time. Touch events are bound to be distributed to the touched window, but events such as no coordinate values are sent only to the main window.
In most cases, the window that hosts the app's content is the main window. However, when the warning view or pop-up input view is popped, they become the main window.

Other references

About window's specific use: How do I create and destroy Windows? How do I read window? Window hierarchy, high level will be covered at low level? Set the main window? window will issue a specific notification when it is hidden, displayed, overwritten, or set as the main window, and can register for notifications of interest if it needs to be processed. Please see the following two articles, I will not be a porter.
http://blog.csdn.net/chengyingzhilian/article/details/7872096
Http://www.cnblogs.com/wendingding/p/3770052.html

My summary:

1,apple's recommendation is that you do not need to create a new window unless you want to project to an external display. So, creating or using window directly is black magic. Do not operate the window unless necessary.
2, directly from the window level to operate, can get unexpected convenience and effect. Interested students, can be in the project to search which of the following third-party code to use this black magic.
3, be sure to remember that window is actually a subclass of UIView.

The more you learn, the faster you do, the more you learn, the better you do.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Behind the Scenes heroes UIWindow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.