UI控件4 UIWindow项目实战

1.UIWindow的对象概念
2.UIWindow和UIView的关系
第一步:info.plist中的 Main storyboard file base name 不要使用系统给的,使用自己写的(点击减号删除)
AppDelegate.m文件实现:

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

//当程序框架初始化成功后 调用此函数
//此函数用来初始化整个程序框架结构
//整个程序对iOS开发工程师的入口函数: didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //入口函数需要写入的内容
    //首先创建一个UIWindow对象
    //整个惩程序中只有一个窗口(UIWindow)对象
    //在程序基本上表示屏幕窗口
    //UIWindow也是继承于UIView
    //UIWindow是一个特殊的UIView
    //initWithFrame:根据一个矩形大小来创建窗口
    //UIScreen:表示屏幕硬件表示类 mainScreen:获得主屏幕的参数
    //bounds 表示屏幕的宽高值
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    //在新版本中 我们的window需要创建一个视图控制器作为UIWindow的跟视图控制器
    self.window.rootViewController = [[UIViewController alloc]init];
    
    //设置背景色
    self.window.backgroundColor = [UIColor whiteColor];
    
    //也可以这样做 在主窗口下添加一个窗口
    //独立创建一个view 添加到window上
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 150, 150)];
    
    view.backgroundColor = [UIColor orangeColor];
    
    //在window上还可以这样操作 :背景视图
    UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 240, 360)];
    
    backView.backgroundColor = [UIColor greenColor];
    
    //把view窗口添加到backView窗口中
    //将backView作为view的父亲视图
    //子视图的坐标是参照父亲视图的坐标而来
    //当父亲视图移动时 所有的子视图也会移动
    [backView addSubview:view];
    
    [self.window addSubview:backView];
    
    //每一个view都有一个window属性
    //打印view的window
    view.window;
    
    backView.window;
    
    //使window有效并显示到屏幕上
    [self.window makeKeyAndVisible];
    
    NSLog(@"%@",view.window);
    NSLog(@"%@",backView.window);
    NSLog(@"%@",self.window);
    //结果
//    2018-06-21 20:19:18.789819+0800 UI控件 6 UIWindow对象[1924:232412] <UIWindow: 0x7f836dc1bf80; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x60000045e210>; layer = <UIWindowLayer: 0x600000227160>>
//    2018-06-21 20:19:18.816380+0800 UI控件 6 UIWindow对象[1924:232412] <UIWindow: 0x7f836dc1bf80; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x60000045e210>; layer = <UIWindowLayer: 0x600000227160>>
//    2018-06-21 20:19:18.816964+0800 UI控件 6 UIWindow对象[1924:232412] <UIWindow: 0x7f836dc1bf80; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x60000045e210>; layer = <UIWindowLayer: 0x600000227160>>

    //.,"
    self.window.rootViewController.view.window;
    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 invalidate graphics rendering callbacks. 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 active 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

猜你喜欢

转载自blog.csdn.net/teropk/article/details/81226921