UIWindow与UIView的创建

// 设置window坐标

    //self.window = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];

    // 这行代码会自动根据需要帮我们生成合适的window大小

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

    // 设置window的颜色

    //self.window.backgroundColor = [UIColor greenColor];

    self.window.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:184 / 255.0 blue:209 / 255.0 alpha:1];

    // 激活window窗口(必须要写)

    [self.window makeKeyAndVisible];

     

    

 

 

    /*                

    iphone 4 4s    320 * 480

    iphone 5 5s 5c 320 * 568

    iphone 6       375 * 667

    iphone 6plus   414 * 736

    */

    

    // window 一个应用程序中通常情况下 只会有一个window

    // 我们所有的展示都是基于这个window

    // UIWindow 继承与 UIView 我们所能用肉眼看得到的所有控件 都是UIView 自身 或者 UIView的子类

    // UIView

    // 初始化视图 给定frame坐标

    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    // 设置颜色

    view1.backgroundColor = [UIColor redColor];

    // 添加到界面上

    // window 此时是 view1 的父视图 view1 window的子视图

    // frame坐标系是子视图 根据 父视图 而定制的

    // 添加子视图 相当于 添加到一个数组当中 我们可以利用这个数组subviews[] 取出子视图

    [self.window addSubview:view1];

    // 释放

    [view1 release];

//    

//    UIView *tempView = self.window.subviews[0];// 此时的tempView 就相当于 view1

//    tempView.backgroundColor = [UIColor orangeColor];

    

    

    // 一个视图只能有一个父视图

    // 父视图可以有很多子视图

    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];

    view2.backgroundColor = [UIColor yellowColor];

    [self.window addSubview:view2];

    [view2 release];

    //[view1 addSubview:view2];

   

    

    // UIColor

    // RGB 是根据三种颜色 决定最后颜色

    // RGB 0 - 255 之间

    // 在实际开发中 RGB的值 都是由设计 给我们提供的 提供一个准确的值 或者一个颜色 如果是颜色 我们可以用测色计来自己查看RGB

//    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

//    CGFloat red = arc4random() % (255 - 1 + 1) + 1;

//    CGFloat green = arc4random() % (255 - 1 + 1) + 1;

//    CGFloat blue = arc4random() % (255 - 1 + 1) + 1

//    view1.backgroundColor = KMyColor;

//    [self.window addSubview:view1];

//    [view1 release];

 

   

    

    

   /*

    UIView *aview = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    aview.backgroundColor = KMyColor;

    // view 矩形 变成曲线型的()

    // 宽和高一致 赋值 一半的值

    [aview.layer setCornerRadius:25];

    [self.window addSubview:aview];

    [aview release];

    */

  

 

 

  

   // center 中心点 也是一个结构体 包含 x y

    // center是相对于父视图来说的 frame 有关

    // frame 发生生变化时 center 也会随之改变

    // center 发生变化时  frame 也会相应改变

    // x = 自身视图.x + 自身视图width / 2

    // y = 自身视图.y + 自身视图height / 2

    /*

    UIView *aview = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    aview.backgroundColor = KMyColor;

    [self.window addSubview:aview];

    [aview release];

    // 查看一个视图的frame

    NSLog(@"x === %f", aview.frame.origin.x);

    NSLog(@"y === %f", aview.frame.origin.y);

    NSLog(@"width === %f",aview.frame.size.width);

    NSLog(@"height === %f",aview.frame.size.height);

    NSLog(@"%@",NSStringFromCGRect(aview.frame));

    NSLog(@"%@",NSStringFromCGPoint(aview.center));

    // 如何改变一个视图的center

    // 通过快速创建一个CGPointMake 结构体

    aview.center = CGPointMake(250, 250);

    NSLog(@"%@",NSStringFromCGRect(aview.frame));

 

  

    UIView *bview = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];

    bview.backgroundColor = [UIColor yellowColor];

    [aview addSubview:bview];

    [bview release];

    NSLog(@"%@",NSStringFromCGPoint(bview.center));

    

    // bounds 是针对于自身的坐标系

    // 当父视图的bounds 发生改变时 不会影响 自身在界面上的位置 只会子视图的参照原点origin发生改变 影响子视图的frame 位置

    // 因为父视图改变之后  会影响到之前的origin 而子视图的位置是根据父视图的origin(0,0) 点来决定的

    // bounds 的改变 不会影响自身的frame center

    // 而自身的frame center 的改变 也不会影响 bounds

    // bounds 是参考自身的坐标系 frame center 是参考父视图为坐标系

    aview.bounds = CGRectMake(10, 10, 100, 100);

    */

    

   

    

    

    /*

    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(25, 25, 100, 50)];

    view1.backgroundColor = [UIColor greenColor];

    [self.window addSubview:view1];

    [view1 release];

    

    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(150, 25, 200, 50)];

    view2.backgroundColor = [UIColor redColor];

    [self.window addSubview:view2];

    [view2 release];

     */

   

    // 根据指定的要求添加视图  最好把需要的数据定义成宏 方便以后好改动

   /*

    for (int i = 0; i < 3; i++)

    {

        // 先创建大视图 大视图里装有两个小视图

        UIView *viewbig = [[UIView alloc]initWithFrame:CGRectMake((KScreenWidth - KBW) / 2, 100 + KYSpace * i, KBW, KVH)];

        viewbig.backgroundColor = [UIColor clearColor];

        [self.window addSubview:viewbig];

        viewbig.tag = 100 + i;

        [viewbig release];

        // 创建两个小视图

        UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLW, KVH)];

        v1.backgroundColor = [UIColor yellowColor];

        [viewbig addSubview:v1];

        [v1 release];

        

        UIView *v2 = [[UIView alloc]initWithFrame:CGRectMake(KLW + KSW, 0, KRW, KVH)];

        v2.backgroundColor = [UIColor greenColor];

        [viewbig addSubview:v2];

        [v2 release];

        

    }

   */

    // 循环创建视图的时候  当我们需要˜找到某一个视图的时候 我们会利用装载子视图的数组 来寻找某一个特定的子视图

     // 或者还可以通过创建tag 添加标识 来寻找子视图                       view.tag = 100;                               [self.window viewWithTag:100]

     // tag 不要设置成0 因为window默认的tag值就是0

     // tag值最好弄成100以上的 防止和数组的下标混淆

    /*

    UIView *tempView = self.window.subviews[1];

    UIView *tempView2 = tempView.subviews[1];

    tempView2.backgroundColor = [UIColor purpleColor];

   */

    /*

    UIView *tempView = [self.window viewWithTag:100];

    UIView *tempView2 = tempView.subviews[1];

    tempView2.backgroundColor = [UIColor purpleColor];

 

    // 还可以通过遍历子视图的方法查找

    for (UIView *tempView in self.window.subviews)

    {

        if (tempView.tag == 101)

        {

            [tempView.subviews[1] setBackgroundColor:[UIColor blackColor]];

            break;

        }

    }

    */

    

    

// UIView 一些基本应用(alpha, hidden, superView, subViews)

    /*

    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];

    // 透明度是0-1 0是完全透明 1是没有透明

    // 如果父视图的透明度发生变化 那么子视图的自身 以及子视图的子视图都会发生改变

    // 针对视图本身的透明度 才会对子视图产生影响 如果是针对字体颜色的透明度 不会对子视图造成任何影响

    // aView.alpha = 0.3;

    

    // 如果父视图的隐藏值为YES 那么子视图的自身 以及子视图的子视图都会发生改变 随之隐藏

    // hidden 的返回值是一个BOOL类型 只有YES NO 默认设置是NO 不隐藏

    aView.hidden = NO;

    aView.backgroundColor = [UIColor redColor];

    [self.window addSubview:aView];

    [aView release];

    

    UIView *bView = [[UIView alloc]initWithFrame:CGRectMake(110,110, 150, 150)];

    bView.backgroundColor = [UIColor greenColor];

    [self.window addSubview:bView];

    [bView release];

    

    UIView *cView = [[UIView alloc]initWithFrame:CGRectMake(120,120, 100, 100)];

    cView.backgroundColor = [UIColor blueColor];

    [self.window addSubview:cView];

    [cView release];

    */

    

 

// UILabel 标签视图 主要用于呈现文本信息 UIView的子类 子类是为了扩展父类中没有的功能

    /*

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 40, 300, 100)];

    label.backgroundColor = [UIColor redColor];

     

    // 设置文本

    label.text = @"用户名用户名用户名用户名用户名用户名用户名用户名";

     

    // 设置文本字体大小

    label.font = [UIFont systemFontOfSize:23];

    // label.font = [UIFont fontWithName:@"草书" size:23];

    // 设置文本颜色

    label.textColor = [UIColor greenColor];

    // 文本的对齐方式(左对齐 右对齐 居中)

    label.textAlignment = NSTextAlignmentLeft;// 居中

    // 设置文本阴影颜色

    label.shadowColor = [UIColor blackColor];

    // 设置文本阴影的偏差度

    label.shadowOffset = CGSizeMake(2, 2);

    // 折行(换行需要和label自身的 宽度 高度 结合使用) 给几 就折成几行

    // 宽度一定 那么高度 必须能够容纳label的文本

    // label.numberOfLines = 2;

    // 自动换行

    label.numberOfLines = 0;// 0 的情况是自动换行 前提是宽度一定 高度能足够满足需求

    [self.window addSubview:label];

    [label release];

    */

    

    

    

// UIView 的常用API方法

    // 移除

    // 把某个视图从父视图上移除掉 那他本身所有的子视图也会随之移除

    // 想移除谁 就用谁去调用

    // [bView removeFromSuperview];

    /*

    UIView *tempView = bView.superview;

    tempView.backgroundColor = [UIColor orangeColor];

    */

    

    // 视图的层级管理

    // 把指定的视图放到最前面 相当于下标是最后一个

    // [self.window bringSubviewToFront:aView];

    

    // 把指定视图放到最后面 相当于下标是第一个

    // [self.window sendSubviewToBack:aView];

    

   

    // 把某个视图插入到指定视图的上方

    // [self.window insertSubview:bView aboveSubview:cView];

    

    // 把某个视图插入到指定视图的下方

    // [self.window insertSubview:cView belowSubview:bView];

    

    // 根据索引值 交换两个视图的位置

    // [self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:2];

    

    // 获取本视图的父视图

    // UIView *superView = [aView superview];

    

    // 获取本视图的所有子视图

    // UIView *subViews = [aView subviews];

    

posted @ 2016-02-23 20:33  mingxing  阅读(214)  评论(0编辑  收藏  举报