手写window

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    self.window.backgroundColor = [UIColor yellowColor];
//    UIViewController *vc = [[UIViewController alloc] init];
//    self.window.rootViewController = vc;
//    [self.window makeKeyAndVisible];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];//系统帮你建画框
    // Override point for customization after application launch.
    
    //自己测试一下 bounds与aplicationFrame的区别
    CGRect bound = [[UIScreen mainScreen]bounds];
    NSLog(@"boundwidth:%f    boundheight:%f ",bound.size.width, bound.size.height);
    NSLog(@"boundx:%f    boundy:%f ",bound.origin.x, bound.origin.y);
    
    CGRect appBound = [[UIScreen mainScreen]applicationFrame];
    NSLog(@"appBoundwidth:%f appBoundheight:%f "
          ,appBound.size.width,appBound.size.height);
    NSLog(@"appBoundx:%f    appBoundy:%f ",appBound.origin.x, appBound.origin.y);
    
    //画第一块画布然涂成蓝色,大小是320 X 100
    CGRect CGone = CGRectMake(0.0, 0.0, 320, 100);//画个矩形,初始化位置与大小
    UIView *v_one = [[UIView alloc]initWithFrame:CGone];//初始化view
    v_one.backgroundColor = [UIColor blueColor];// 涂成蓝色
    //[self.window addSubview:v_one];//直接加到画框上
    
    //第二块注意它的位置
    CGRect CGtwo = CGRectMake(0.0, 100, 160, 100);//画个矩形、初始化位置与大小
    UIView *v_two = [[UIView alloc]initWithFrame:CGtwo];//初始化view
    v_two.backgroundColor = [UIColor redColor];//涂成红色
    //[self.window addSubview:v_two];//叠加到画框
    //第三块注意他的位置
    CGRect CGthree = CGRectMake(160, 100, 160, 100);//
    UIView *v_three = [[UIView alloc]initWithFrame:CGthree];//
    v_three.backgroundColor = [UIColor greenColor];//
    //[self.window addSubview:v_three];//
    //第四块注意它的位置
    CGRect CGfour = CGRectMake(10, 10, 320, 200);//
    UIView *v_four = [[UIView alloc]initWithFrame:CGfour];//
    v_four.backgroundColor = [UIColor orangeColor];//
    //[self.window addSubview:v_four];//
    //第五块,计算一下它的位置,看看它的效果,
    //你可以让试一下把这段代码移到第一快初始化的上面试试,会有意想不到的效果
    CGRect CGfive = CGRectMake(100, 150, 160, 200);
    UIView *v_five = [[UIView alloc]initWithFrame:CGfive];
    v_five.backgroundColor = [UIColor yellowColor];
    //[self.window addSubview:v_five];
    //self.window.backgroundColor = [UIColor grayColor];//
    
//    ViewController *vc = [[ViewController alloc] init];
//    self.window.rootViewController = vc;
//    [self.window makeKeyAndVisible];//
    
    UIViewController *vc = [[UIViewController alloc] init];
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 414, 300)];
    UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 414, 730)];
   
    [v addSubview:v_one];
    [v addSubview:v_two];
    [v addSubview:v_three];
    [v1 addSubview:v_four];
    [v1 addSubview:v_five];
    [vc.view addSubview:v];
    [vc.view addSubview:v1];
    v.backgroundColor =[UIColor whiteColor];
    v1.backgroundColor = [UIColor purpleColor];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];

    
    
    return YES;
}

猜你喜欢

转载自chenggi102.iteye.com/blog/2307645