当前位置:首页 > Windows程序 > 正文

UIWindow与程序运行周期

2021-03-24 Windows程序

window 相当于一个画板,要想展现自己想要的图像或者图形,需要把画的东西画在window这个画板上。window继承于UIView,在ui中,所有的能看得到的东西,都是继承于UIView。在iOS中,通常用UIWindow来表示窗口,每个app都要把要展现的东西都写在UIWindow上。通常,一个app只创建一个UIWindow对象。

创建UIWindow对象

#import "AppDelegate.h"

// 当点击APP图标,开始讲屏幕的显示权交给我们的APP时 但是什么东西都没有显示的时候,会调用这个方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

// 创建window对象,,初始化时设置window的位置和大小与屏幕相同。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//  x 指的是  离左边的距离   y指的是 离上边的距离  宽度  高度 是本身

// bouns 和frame 的区别体现在起点上   frame相对于父控件的   bounds 标示自己的

NSLog(@"x=%g , y=%g , width=%g , height=%g",self.window.frame.origin.x,self.window.frame.origin.y,self.window.frame.size.width,self.window.frame.size.height);

   // 给window设置背景颜色

self.window.backgroundColor = [UIColor whiteColor];

// 让我们的window显示

[self.window makeKeyAndVisible];

}

//告诉delegate应用程序即将进入非活跃状态(暂停游戏、停止timer等)...

- (void)applicationWillResignActive:(UIApplication *)application{}

//告诉delegate应用程序已经进入了后台(存储用户数据、释放一些共享资源、停止timer等)...

- (void)applicationDidEnterBackground:(UIApplication *)application{}

//告诉delegate应用程序即将进入前台(取消所有进入后台时暂停的任务)...

- (void)applicationWillEnterForeground:(UIApplication *)application{}

//告诉delegate应用程序已经进入活跃状态(重新执行被暂停的任务)...

- (void)applicationDidBecomeActive:(UIApplication *)application{}

//当应用程序即将终止时调用。如果适当的话,保存数据。

- (void)applicationWillTerminate:(UIApplication *)application{}

解释:

1、initWithFrame:[[UIScreen mainScreen] bounds],初始化window,使这个window跟屏幕一样大小。

2、backgroundColor,设置背景色

3、makeKeyAndVisible,把window设置成可显示的。

UIWindow与程序运行周期

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/66711.html