源码03-02-03-04-UIWindow

 

自定义窗口,不使用系统的storyboard

 

 

//
//  main.m
//  03-UIWindow
#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

// main -> UIApplicationMain
/*
 UIApplicationMain
 1.创建UIApplication
 2.创建UIApplicationDelegate,并且成为UIApplication代理
 3.开启主运行循环,保持程序一直在运行
 4.加载info.plist,判断有没有指定main.stroyboard,指定了就加载
 
 
 加载main.stroyboard做的事情
 1.创建窗口
 2.加载main.storyboard,并且加载main.storyboard指定的控制器
 3.把新创建的控制器作为窗口的跟控制器,让窗口显示出来
 
 */

 

//
//  AppDelegate.m
//  03-UIWindow
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

// 程序启动完成的时候
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 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];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // 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.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 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.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // 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.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 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.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

 

 

04-UIWindow补充

//  AppDelegate.m
//  04-UIWindow补充
#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIWindow *window1;

@end

@implementation AppDelegate

//- (void)setWindow:(UIWindow *)window
//{
//    _window = window;
//    [UIApplication sharedApplication].windows
//}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window1 = [[UIWindow alloc] initWithFrame:CGRectMake(50, 50, 250, 250)];
    
    self.window1.backgroundColor = [UIColor yellowColor];
    
    // 设置窗口的层级关系
    self.window1.windowLevel = UIWindowLevelStatusBar;
    
    [self.window1 makeKeyAndVisible];

    
    // 窗口是有层级关系
    // UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.windowLevel = UIWindowLevelAlert;
    
    self.window.backgroundColor = [UIColor redColor];
    
    [self.window makeKeyAndVisible];
    
    
    
    return YES;
}

- (void)keyboardIsWindow
{
    // 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor yellowColor];
    
    // 判断下键盘是不是窗口
//    NSLog(@"%@",application.windows);
    // 如何才能弹出键盘
    // 如果需要看到键盘,必须要把textField添加到一个View上面
    UITextField *textField = [[UITextField alloc] init];
    _textField = textField;
    [textField becomeFirstResponder];
    [self.window addSubview:textField];
    
//    NSLog(@"%@",application.windows);
    
    // 显示窗口
    [self.window makeKeyAndVisible];

}

// 窗口:键盘,状态栏也是窗口

#pragma mark - 窗口的显示
- (void)windowWithVisible
{
    // 1.创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    //    self.window.backgroundColor = [UIColor greenColor];
    
    // application.windows:只要一给delegate的window属性赋值,就会添加到windows数组。
//    NSLog(@"%@",application.windows);
    
    // 2.显示窗口
    // makeKeyAndVisible:成为app的主窗口并且显示
    [self.window makeKeyAndVisible];
//    NSLog(@"%@",application.windows);
    //    self.window.hidden = NO;
    NSLog(@"%@",self.window);

}
- (void)applicationWillResignActive:(UIApplication *)application {
    // 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.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 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.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // 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.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 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.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

 

posted @ 2017-03-14 16:54  laugh  阅读(138)  评论(0编辑  收藏  举报