UIWindow

作者: 蒋昉霖 | 来源:发表于2016-03-08 10:37 被阅读0次

xib创建

// 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];


    // 通过xib创建控制器
    ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];


    self.window.rootViewController = vc;


    [self.window makeKeyAndVisible];
    ```

## storyboard创建
```objc
    // 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];


    // 创建窗口的跟控制器
    // 加载storyboard
    // storyboard文件名,不需要带后缀
    // nil:  [NSBundle mainBundle]
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    // 通过storyboard创建控制器
    // instantiateInitialViewController:加载箭头指向的控制器
//    UIViewController *vc = [storyboard instantiateInitialViewController];

    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"green"];

    NSLog(@"%@",[vc class]);
    self.window.rootViewController = vc;

    // 显示窗口
    [self.window makeKeyAndVisible];

代码创建

    // 1.创建窗口,注意窗口必须要有尺寸,尺寸跟屏幕一样大的尺寸,窗口不要被释放
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];

    // 2.创建窗口的跟控制器
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];

    [vc.view addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];

    // 如果设置窗口的跟控制器,默认就会把控制器的view添加到窗口上
    // 设置窗口的跟控制器,默认就有旋转功能
    self.window.rootViewController = vc;

//    [self.window addSubview:vc.view];

    // 3.显示窗口
    [self.window makeKeyAndVisible];
    ```


window是有层级的,而且不是只有一个



AppDelegate
```objc
// 程序启动完成的时候调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"%s",__func__);
    return YES;
}

// 当app失去焦点的时候调用
- (void)applicationWillResignActive:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

// app进入后台的时候调用
// app忽然打断的时候,在这里保存一些需要用到的数据
- (void)applicationDidEnterBackground:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


// app进入即将前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

// 当app获取到焦点的时候调用,意味着app可以与用户交互
- (void)applicationDidBecomeActive:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

// app被关闭的时候调用
- (void)applicationWillTerminate:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


// app接收到内存警告的时候调用
// 清空图片的缓存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    NSLog(@"%s",__func__);
}

相关文章

  • UIWindow

    目录:1、UIWindow简介2、UIWindow的创建3、获取UIWindow 1、UIWindow简介 UIW...

  • UIWindow 原理与巧妙使用 makeKeyAndVisib

    - UIWindow 简介- UIWindow 概述- 我们可以使用 UIWindow 来作什么?- makeKe...

  • UIWindow、UIView、CALayer

    UIWindow的windowLevel属性 UIWindow简单介绍 UIWindow详解及踩坑 UIWindo...

  • UIWindow讲解

    1.UIWindow & UIWindowLevel 一、简单介绍UIWindow是什么? UIWindow是一种...

  • iOS开发之UIWindow的使用

    一、UIWindow简介 UIWindow是最顶级的界面容器。UIWindow继承自UIView。 UIWindo...

  • iOS 开发- UI篇-UIWindow介绍

    UIWindow 简单介绍原文链接? iOS开发UI篇—UIWindow简单介绍 一、简单介绍 UIWindow是...

  • iOS --- UI 简单总结

    代码创建UIWindow对象 Xcode7之后使用代码创建UIWindow对象: //创建UIWindow对象 s...

  • iOS关于UIWindow

    UIWindow简介: 在iOS App中,UIWindow是最顶层的界面内容,我们使用UIWindow和UIVi...

  • UIWindow 知识梳理

    UIWindow简介 在iOS开发中,UIWindow和UIView一样都是用来呈现界面的。UIWindow并不包...

  • UIWindow的基本使用

    二、使用UIWindow 1、简介在iOS App中,UIWindow是最顶层的界面内容,我们使用UIWindow...

网友评论

    本文标题:UIWindow

    本文链接:https://www.haomeiwen.com/subject/kglzkttx.html