Home>

uiwindow is a special uiview, usually there will only be one uiwindow in an app.

After the ios program starts,The first view control created is uiwindow, then the controller's view is created, and finally the controller's view is added to uiwindow, so the controller's view is displayed on the screen.

The reason why an ios program can be displayed on the screen,It's all because it has uiwindow. In other words, without uiwindow, you cannot see any ui interface.

How to get uiwindow

(1) [uiapplication sharedapplication] .windows The list of uiwindows opened in this application, so that you can access any uiview object in the application (the keyboard that usually pops up when you enter textJust in a new uiwindow);

(2) [uiapplication sharedapplication] .keywindow (get the application's main window) is used to receive keyboard and non-touch message events of uiwindow, and only one uiwindow at any time in the program is keywindow;

Note:After code verification,Non-keywindow can also accept keyboard messages;

Tip:If you cannot enter text in a text box inside a uiwindow,It may be because this uiwindow is not a keywindow;

(3) view.window gets the uiwindow where a uiview is located.

uiwindowlevel

We know that uiwindow has three levels,They are normal, statusbar, alert. Output their three levels of values,We found that from left to right are 0, 1000, 2000, which means that the normal level is the loweststatusbar is at intermediate level,The alert level is the highest.And usually the interface of our program is at the normal level.The status bar at the top of the system should be at the statusbar level. These uiactionsheet and uialertview are usually used to interrupt the normal process.Remind users to wait, etc.It is therefore at the alert level.

According to the window display priority principle,The highest level will be displayed on the top level,The lower levels are below,The view normally displayed by our program is at the bottom;

keywindow

The official documentation explains this as "the key window is the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window."keywindow is used to receive keyboard and non-touch messages.And only one window at a time in the program is keywindow.

Observing the uiwindow document, we can find that there are four notifications about window changes:

Uiwindowdidbecomevisiblenotification

Uiwindowdidbecomehiddennotification

Uiwindowdidbecomekeynotification

Uiwindowdidresignkeynotification

of The object in the four notification objects represents the window object that is currently displayed (hidden) and has become a keywindow (non-keywindow) window. The userinfo is empty.So we can register these four messages,Print the information again to observe the changes of keywindow and the display and hidden changes of the window

The process of turning into keywindow is like this

1. The program's default window is displayed first

2. The default window becomes keywindow again

3.Alertview window is displayed

4. The default window becomes keywindow

5. Eventually the window of alertview becomes keywindow

Ios8 starts to change the bounds of uiwindow (window itself has rotated)

Windows 7 bounds will not change with direction before iOS 7,But after iOS 8, with the rotation of the device direction,window.bounds.size.width and window.bounds.size.height will change accordingly.

Make a very simple test,code show as below:

-(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;
}
</p>
<p>
-(void) orientationchanged:(nsnotification *) noti {
 uideviceorientation orientation=[uidevice currentdevice] .orientation;
 nsstring * orientationdes=nil;
 switch (orientation) {
 case uideviceorientationlandscapeleft:
 orientationdes [email protected]"uiinterfaceorientationlandscaperight";
 break;
 case uideviceorientationlandscaperight:
 orientationdes [email protected]"uiinterfaceorientationlandscapeleft";
 break;
 case uideviceorientationportrait:
 orientationdes [email protected]"uiinterfaceorientationportrait";
 break;
 case uideviceorientationportraitupsidedown:
 orientationdes [email protected]"uiinterfaceorientationportraitupsidedown";
 break;
 default:
 orientationdes [email protected]"";
 break;
 }
 nslog (@ "system ver:%@, \ rorientaion:%@, \ rwindow bounds:%@", [uidevice currentdevice] .systemversion, orientationdes, nsstringfromcgrect (self.window.bounds));
}

The sample code is simple,Create a new project,Then add the above code directly in the delegate.

result ios 8 running results are:

2014-06-04 09:26:32.016 svios8 [4143:61114] system ver:8.0,</p>
<p>
orientaion:uiinterfaceorientationlandscaperight,</p>
<p>
window bounds:{{0, 0}, {320, 480}}
</p>
<p>
2014-06-04 09:26:34.788 svios8 [4143:61114] system ver:8.0,</p>
<p>
orientaion:uiinterfaceorientationportrait,</p>
<p>
window bounds:{{0, 0}, {480, 320}}
</p>
<p>
2014-06-04 09:26:35.791 svios8 [4143:61114] system ver:8.0,</p>
<p>
orientaion:uiinterfaceorientationlandscapeleft,</p>
<p>
window bounds:{{0, 0}, {320, 480}}
</p>
<p>
2014-06-04 09:26:47.468 svios8 [4143:61114] system ver:8.0,</p>
<p>
orientaion:uiinterfaceorientationportraitupsidedown,</p>
<p>
window bounds:{{0, 0}, {480, 320}}

result ios 7 and earlier versions run as follows:

2014-06-04 09:39:00.527 svios8 [4380:70b] system ver:7.0.3,</p>
<p>
orientaion:uiinterfaceorientationlandscaperight,</p>
<p>
window bounds:{{0, 0}, {320, 480}}
</p>
<p>
2014-06-04 09:39:00.895 svios8 [4380:70b] system ver:7.0.3,</p>
<p>
orientaion:uiinterfaceorientationportrait,</p>
<p>
window bounds:{{0, 0}, {320, 480}}
</p>
<p>
2014-06-04 09:39:01.225 svios8 [4380:70b] system ver:7.0.3,</p>
<p>
orientaion:uiinterfaceorientationlandscapeleft,</p>
<p>
window bounds:{{0, 0}, {320, 480}}
</p>
<p>
2014-06-04 09:39:11.004 svios8 [4380:70b] system ver:7.0.3,</p>
<p>
orientaion:uiinterfaceorientationportraitupsidedown,</p>
<p>
window bounds:{{0, 0}, {320, 480}}

Compared Through comparison, we can clearly see the change of uiwindow's strategy when dealing with rotation in iOS 8.Although there will be bugs in the existing project layout due to differences from the previous version, you can see that the processing method in ios 8 is more in line with our expectations,We obtain width<height in the vertical direction and width>height in the horizontal direction.

Off topic, whether it's iOS 7 or earlier,As well as the latest iOS 8, all bounds of the viewcontroller are correct,So just stick to the principle "All layouts are based on vc.view.bounds layout, then your app's display will be normal."

ios
  • Previous C # implements methods to read DataSet data and display it in ListView control
  • Next Detailed explanation of PV statistics function of Django blog using Celery