一、UIWindow是一种特殊的UIView,通常在一个程序中只会有一个UIWindow,但可以手动创建多个UIWindow,同时加到程序里面。UIWindow在程序中主要起到三个作用:
1、作为容器,包含app所要显示的所有视图
2、传递触摸消息到程序中view和其他对象
3、与UIViewController协同工作,方便完成设备方向旋转的支持
二、通常我们可以采取两种方法将view添加到UIWindow中:
1、addSubview
直接将view通过addSubview方式添加到window中,程序负责维护view的生命周期以及刷新,但是并不会为去理会view对应的 ViewController,因此采用这种方法将view添加到window以后,我们还要保持view对应的ViewController的有效性, 不能过早释放。
2、rootViewController
rootViewController时UIWindow的一个遍历方法,通过设置该属性为要添加view对应的 ViewController,UIWindow将会自动将其view添加到当前window中,同时负责ViewController和view的生命 周期的维护,防止其过早释放
三、WindowLevel
UIWindow在显示的时候会根据UIWindowLevel进行排序的,即Level高的将排在所有Level比他低的层级的前面。下面我们来看UIWindowLevel的定义:
const UIWindowLevel UIWindowLevelNormal;
const UIWindowLevel UIWindowLevelAlert;
const UIWindowLevel UIWindowLevelStatusBar;
typedef CGFloat UIWindowLevel;
IOS系统中定义了三个window层级,其中每一个层级又可以分好多子层级(从UIWindow的头文件中可以看到成员变量CGFloat _windowSublevel;),不过系统并没有把则个属性开出来。UIWindow的默认级别是UIWindowLevelNormal,我们打印输出这三个level的值分别如下:
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
这样印证了他们级别的高低顺序从小到大为Normal < StatusBar < Alert,下面请看小的测试代码:
TestWindowLevel
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2 3 { 4 5 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 6 7 self.window.backgroundColor = [UIColor yellowColor]; 8 9 [self.window makeKeyAndVisible]; 10 11 12 13 UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 14 15 normalWindow.backgroundColor = [UIColor blueColor]; 16 17 normalWindow.windowLevel = UIWindowLevelNormal; 18 19 [normalWindow makeKeyAndVisible]; 20 21 22 23 CGRect windowRect = CGRectMake(50, 24 25 50, 26 27 [[UIScreen mainScreen] bounds].size.width - 100, 28 29 [[UIScreen mainScreen] bounds].size.height - 100); 30 31 UIWindow *alertLevelWindow = [[UIWindow alloc] initWithFrame:windowRect]; 32 33 alertLevelWindow.windowLevel = UIWindowLevelAlert; 34 35 alertLevelWindow.backgroundColor = [UIColor redColor]; 36 37 [alertLevelWindow makeKeyAndVisible]; 38 39 40 41 UIWindow *statusLevelWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 50, 320, 20)]; 42 43 statusLevelWindow.windowLevel = UIWindowLevelStatusBar; 44 45 statusLevelWindow.backgroundColor = [UIColor blackColor]; 46 47 [statusLevelWindow makeKeyAndVisible]; 48 49 50 51 NSLog(@"Normal window level: %f", UIWindowLevelNormal); 52 53 NSLog(@"Normal window level: %f", UIWindowLevelAlert); 54 55 NSLog(@"Normal window level: %f", UIWindowLevelStatusBar); 56 57 58 59 return YES; 60 61 }
我们可以注意到两点:
1)我们生成的normalWindow虽然是在第一个默认的window之后调用makeKeyAndVisible,但是仍然没有显示出来。这说明当Level层级相同的时候,只有第一个设置为KeyWindow的显示出来,后面同级的再设置KeyWindow也不会显示。
2)statusLevelWindow在alertLevelWindow之后调用makeKeyAndVisible,淡仍然只是显示在alertLevelWindow的下方。这说明UIWindow在显示的时候是不管KeyWindow是谁,都是Level优先的,即Level最高的始终显示在最前面。
二、区别
1、UIScreen可以获取设备屏幕的大小。
2 7 18 整个屏幕的大小 {{0, 0}, {320, 480}} 19 20 CGRect 21 22 bounds = [UIScreen mainScreen].bounds; 23 24 NSLog(@"UIScreen 25 26 bounds: %@", 27 28 NSStringFromCGRect(bounds)); 29 30 31 32 // 33 34 应用程序窗口大小 {{0, 20}, {320, 460}} 35 36 CGRect 37 38 applicationFrame = [UIScreen mainScreen].applicationFrame; 39 40 NSLog(@"UIScreen 41 42 applicationFrame: %@", 43 44 NSStringFromCGRect(applicationFrame));
2、UIView对象定义了一个屏幕上的一个矩形区域,同时处理该区域的绘制和触屏事件。?可以在这个区域内绘制图形和文字,还可以接收用户的操作。一个UIView的实例可以包含和管理若干个子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对象是所有UIView的根,管理和协调的应用程序的显示?UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件。
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、UIViewController对象负责管理所有UIView的层次结构,并响应设备的方向变化。
AppDelegate.m
1 2 3 4 5 6 7 8 9 10 |
- (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; } |
三、完整的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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])); } } |