Learn more about the UIWindow associated with IOS development _ios

Source: Internet
Author: User

UIWindow is a special kind of uiview, which usually has only one uiwindow in an app.

After the iOS program is started, the first view control created is UIWindow, then creates the controller's view, and then adds the controller's view to the UIWindow, so the controller view is displayed on the screen.

An iOS program can be displayed on the screen simply because it has uiwindow. It also says that without UIWindow, no UI interface is visible.

How to get UIWindow

(1) [UIApplication sharedapplication].windows a list of UIWindow opened in this application so that you can contact any one of the UIView objects in the application (usually type the keyboard that pops into the text, In the midst of a new UIWindow);

(2) [UIApplication Sharedapplication].keywindow (Gets the main window of the application) is used to receive uiwindow of message events for keyboards and non-touch classes. And every moment in the program can only have one uiwindow is Keywindow;

Note: After the code verification, the Keywindow can also accept the keyboard message;

Tip: If a text box inside a UIWindow cannot enter text, it may be because the UIWindow is not keywindow;

(3) View.window obtains the UIWindow of a uiview.

Uiwindowlevel

We know there are three levels of UIWindow, Normal, StatusBar, Alert. Output their three-level values, we found from left to right is 0,1000,2000, that is, the normal level is the lowest, statusbar in the middle, the highest alert level. And usually our program interface is at the normal level, the system at the top of the status bar should be at the statusbar level, Uiactionsheet and uialertview these are usually used to interrupt the normal process, to remind users and other operations, Therefore, at the alert level.

According to the window display level priority principle, the level of high will be displayed at the top level, low-level below, our program is normally displayed in the bottom of the view;

Keywindow

This is explained in the official document, "The key window is the one of this is designated to receive keyboard and Non-touch related events." Only one window in a time will be the key window. That is, Keywindow is a specified message to receive the keyboard as well as a non touch class, and a window is keywindow at every moment in the program.

Looking at UIWindow's documentation, we can see that there are four notices about window changes:

Uiwindowdidbecomevisiblenotification

Uiwindowdidbecomehiddennotification

Uiwindowdidbecomekeynotification

Uiwindowdidresignkeynotification

object in these four notification objects represents a Window object that is currently displayed (hidden) and has become keywindow (not Keywindow), where the userinfo is empty. So we can register this four messages, and then print the information to observe the Keywindow changes and window display, hidden changes

The process of becoming keywindow is like this

1. The default window of the program is shown first

2. The default window again becomes Keywindow

The 3.AlertView window shows up

4. The default window becomes Keywindow

5. The final Alertview window becomes Keywindow

IOS8 begins to change uiwindow bounds (window itself has rotated)

The bounds of Windows before iOS 7 will not change with the direction, but after iOS 8, window.bounds.size.width and Window.bounds.size.height will change accordingly as the device direction rotates.

To do a very simple test, the code is as follows:

Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
Override point for customization after application launch.

[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (orientationchanged:)
Name:uideviceorientationdidchangenotification
Object:nil];

return YES;
}

-(void) orientationchanged: (nsnotification*) Noti {

uideviceorientation orientation = [Uidevice currentdevice].orientation;
NSString *orientationdes = nil;
Switch (orientation) {
Case Uideviceorientationlandscapeleft:
Orientationdes = @ "Uiinterfaceorientationlandscaperight";
Break
Case Uideviceorientationlandscaperight:
Orientationdes = @ "Uiinterfaceorientationlandscapeleft";
Break
Case UIDEVICEORIENTATIONPORTRAIT:
Orientationdes = @ "uiinterfaceorientationportrait";
Break
Case Uideviceorientationportraitupsidedown:
Orientationdes = @ "Uiinterfaceorientationportraitupsidedown";
Break
Default
Orientationdes = @ "";
Break
}

NSLog (@ "System ver:%@, \rorientaion:%@, \rwindow bounds:%@",
[Uidevice Currentdevice].systemversion,
Orientationdes,
Nsstringfromcgrect (self.window.bounds));
}


The sample code is simple, create a new project, and then add the above code directly in the delegate.

The results on IOS 8 run as follows:

Copy Code code as follows:

2014-06-04 09:26:32.016 svios8[4143:61114] System ver:8.0,

Orientaion:uiinterfaceorientationlandscaperight,

Window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:26:34.788 svios8[4143:61114] System ver:8.0,

Orientaion:uiinterfaceorientationportrait,

Window bounds: {{0, 0}, {480, 320}}

2014-06-04 09:26:35.791 svios8[4143:61114] System ver:8.0,

Orientaion:uiinterfaceorientationlandscapeleft,

Window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:26:47.468 svios8[4143:61114] System ver:8.0,

Orientaion:uiinterfaceorientationportraitupsidedown,

Window bounds: {{0, 0}, {480, 320}}


The results of the IOS 7 and previous versions run as follows:

Copy Code code as follows:
2014-06-04 09:39:00.527 svios8[4380:70b] System ver:7.0.3,

Orientaion:uiinterfaceorientationlandscaperight,

Window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:39:00.895 svios8[4380:70b] System ver:7.0.3,

Orientaion:uiinterfaceorientationportrait,

Window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:39:01.225 svios8[4380:70b] System ver:7.0.3,

Orientaion:uiinterfaceorientationlandscapeleft,

Window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:39:11.004 svios8[4380:70b] System ver:7.0.3,

Orientaion:uiinterfaceorientationportraitupsidedown,

Window bounds: {{0, 0}, {320, 480}}


By contrast, we can see clearly that the UIWindow in iOS 8 is changing the policy while processing the rotation, although the bugs that are present in the layout of the existing project are different from the previous version, but you can see that the processing in iOS 8 is more in line with our expectations, We get the width < height on the vertical, and the width > height in the transverse direction, which conforms to the WYSIWYG principle.
 
Digression, whether it's iOS 7 or the previous version, and the latest iOS 8, all viewcontroller bounds are correct, so just stick to one principle. All layouts are based on the VC.view.bounds layout, so your app's display is all right. The

Related Article

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.