UIWindow & Uiwindowlevel Detailed
First, UIWindow is a special kind of uiview, usually in a program will only have a uiwindow, but you can manually create multiple UIWindow, and add to the program inside. UIWindow plays a major role in the process of three:
1, as a container, contains all the views that the app wants to display
2. Pass the touch message to the program view and other objects
3, with uiviewcontroller work together, to facilitate the completion of equipment direction rotation support
Second, there are usually two ways to add a view to the UIWindow:
1, Addsubview
The view is added directly to the window by Addsubview, the program is responsible for maintaining the view's life cycle and refreshing, but not for the viewcontroller of the view, so after adding the view to the window in this way, We also need to maintain the validity of the viewcontroller of the view and not release it prematurely.
2, Rootviewcontroller
Rootviewcontroller a traversal method of UIWindow, by setting this property to add a view corresponding to the Viewcontroller,uiwindow will automatically add its view to the current window, Also responsible for Viewcontroller and view life cycle maintenance to prevent premature release
Third, Windowlevel
The UIWindow will be sorted according to Uiwindowlevel when displayed, that is, the level will be in front of all levels below him. Let's look at the definition of Uiwindowlevel:
Const Uiwindowlevel Uiwindowlevelnormal;
Const Uiwindowlevel Uiwindowlevelalert;
Const Uiwindowlevel Uiwindowlevelstatusbar;
typedef cgfloat Uiwindowlevel;
The iOS system defines three window hierarchies, each of which can be divided into sub-tiers (the member variable CGFloat _windowsublevel can be seen from the UIWindow header file), but the system does not have a property open. The default level for UIWindow is Uiwindowlevelnormal, and we print out the values for these three levels, respectively, as follows:
2012-03-27 22:46:08.752 uiviewsample[395:f803] Normal window level:0.000000
2012-03-27 22:46:08.754 uiviewsample[395:f803] Normal window level:2000.000000
2012-03-27 22:46:08.755 uiviewsample[395:f803] Normal window level:1000.000000
This confirms their level of high and low order from small to large for normal < StatusBar < Alert, below to see the Little test code:
Testwindowlevel
1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions2 3 {4 5Self.window =[[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];6 7Self.window.backgroundColor =[Uicolor Yellowcolor];8 9 [Self.window makekeyandvisible];Ten One A -UIWindow *normalwindow =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; - theNormalwindow.backgroundcolor =[Uicolor Bluecolor]; - -Normalwindow.windowlevel =Uiwindowlevelnormal; - + [Normalwindow makekeyandvisible]; - + A atCGRect Windowrect = CGRectMake ( -, - - -, - -[[UIScreen Mainscreen] Bounds].size.width- -, - in[[UIScreen Mainscreen] Bounds].size.height- -); - toUIWindow *alertlevelwindow =[[UIWindow alloc] initwithframe:windowrect]; + -Alertlevelwindow.windowlevel =Uiwindowlevelalert; the *Alertlevelwindow.backgroundcolor =[Uicolor Redcolor]; $ Panax Notoginseng [Alertlevelwindow makekeyandvisible]; - the + AUIWindow *statuslevelwindow = [[UIWindow alloc] Initwithframe:cgrectmake (0, -, the, -)]; the +Statuslevelwindow.windowlevel =Uiwindowlevelstatusbar; - $Statuslevelwindow.backgroundcolor =[Uicolor Blackcolor]; $ - [Statuslevelwindow makekeyandvisible]; - the - WuyiNSLog (@"Normal window level:%f", uiwindowlevelnormal); the -NSLog (@"Normal window level:%f", Uiwindowlevelalert); Wu -NSLog (@"Normal window level:%f", Uiwindowlevelstatusbar); About $ - - returnYES; - A}
We can notice two points:
1) The Normalwindow we generate is called makekeyandvisible after the first default window, but it is still not displayed. This means that when level levels are the same, only the first one is set to Keywindow, and the Keywindow of the subsequent siblings are not displayed.
2) Statuslevelwindow call makekeyandvisible after Alertlevelwindow, the light is still only shown below the Alertlevelwindow. This shows that the UIWindow at the time of display, regardless of who Keywindow is, is the level priority, that is, the highest level is always displayed at the front.
Second, the difference
1, UIScreen can get the size of the device screen.
2 7 -The size of the entire screen {{0,0}, { the,480}} + - CGRect + ABounds =[UIScreen mainscreen].bounds; at -NSLog (@"UIScreen - - bounds:%@", - - nsstringfromcgrect (bounds)); in - to + // - theapplication window Size {{0, -}, { the,460}} * $ CGRectPanax Notoginseng -Applicationframe =[UIScreen mainscreen].applicationframe; the +NSLog (@"UIScreen A the applicationframe:%@", + -Nsstringfromcgrect (Applicationframe));
2. The UIView object defines a rectangular area on the screen and handles drawing and touch events for that area. You can draw graphics and text within this area, and you can also receive user actions. An instance of UIView can contain and manage several sub-uiview.
Viewcontroller.m
1 2 3 4 5 6 7 |
- (void) Viewdidappear: (BOOL) animated { [Super Viewdidappear:animated]; uiview* MyView = [[UIView alloc] Initwithframe:cgrectmake (10, 10, 100, 100)]; Myview.backgroundcolor=[uicolor Redcolor]; [Self.view Addsubview:myview]; } |
3. UIWindow object is the display of all UIView's root, managed and coordinated applications? The UIWindow class is a subclass of UIView and can be considered a special uiview. A generic application has only one UIWindow object, and even if there are multiple UIWindow objects, only one UIWindow can accept the user's touchscreen event.
Appdelegate.m
1 2 3 4 5 6 7 8 |
- (BOOL) Application: (uiapplication *) Application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions { UIWindow *mywindow = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; Mywindow.backgroundcolor=[uicolor Whitecolor]; [Mywindow Makekeyandvisible]; _window = Mywindow; Return YES; } |
4. The Uiviewcontroller object is responsible for managing all UIView hierarchies and responding to changes in the direction of the device.
Appdelegate.m
1 2 3 4 5 6 7 8 9 |
- (BOOL) Application: (uiapplication *) Application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions { UIWindow *mywindow = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; Mywindow.backgroundcolor=[uicolor Whitecolor]; Uiviewcontroller *myviewcontroller = [[Uiviewcontroller alloc] Initwithnibname:nil Bundle:nil]; Mywindow.rootviewcontroller = Myviewcontroller; [Mywindow Makekeyandvisible]; _window = Mywindow; Return YES; } |
Three, the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// ViewController.h #import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller @end // Viewcontroller.m #import "ViewController.h" @implementation Viewcontroller - (void) Viewdidappear: (BOOL) animated { [Super Viewdidappear:animated]; uiview* MyView = [[UIView alloc] Initwithframe:cgrectmake (10, 10, 100, 50)]; Myview.backgroundcolor=[uicolor Redcolor]; [Self.view Addsubview:myview]; } - (BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation { Return YES; } @end // AppDelegate.h #import <UIKit/UIKit.h> @class Viewcontroller; @interface Appdelegate:uiresponder <UIApplicationDelegate> @property (Strong, Nonatomic) UIWindow *window; @property (Strong, Nonatomic) Viewcontroller *viewcontroller; @end // Appdelegate.m #import "AppDelegate.h" #import "ViewController.h" @implementation Appdelegate @synthesize window = _window; @synthesize Viewcontroller = _viewcontroller; - (BOOL) Application: (uiapplication *) Application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions { Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; Self.viewcontroller = [[Viewcontroller alloc] Initwithnibname:nil Bundle:nil]; Self.window.rootViewController = Self.viewcontroller; [Self.window Makekeyandvisible]; Return YES; } @end // Main.m #import <UIKit/UIKit.h> #import "AppDelegate.h" Int Main (int ARGC, Char *argv[]) { @autoreleasepool { Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class])); } } |
Ui-day01--uiwindow & Uiwindowlevel (Thu)