iOS学习笔记5-UIView

常见的iphone 尺寸:

     iphone 3gs 3.5: 320 x 480 @1x

     iphone 4 & 4s 3.5 320 x 480 @2x 640 X 960

     iphone 5 5c 5s 320X 568 @2x 640 X 1136

     iphone 6 4.7  375x 667 @2x 750x1344

     iphone 6s 5.5 414 x 736 @3x 1242x2208

    


UIView 常用方法:

    UIView* view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(10, 30, 350, 627);//状态栏高度为20px
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    NSLog(@"x: %f, y: %f, w: %f, h: %f", view1.frame.origin.x, view1.frame.origin.y, view1.frame.size.width, view1.frame.size.height);
    // bound - 边框大小,x,y永远为零
    NSLog(@"x: %f, y: %f, w: %f, h: %f", view1.bounds.origin.x, view1.bounds.origin.y, view1.bounds.size.width, view1.bounds.size.height);
    
    // center - 中心点
    NSLog(@"center X: %f, Y: %f", view1.center.x, view1.center.y);
    
    
    //父视图
    UIView* superView = view1.superview;
    superView.backgroundColor = [UIColor greenColor];
    // 坐标根据父视图来设置的,不会垮层
    
    UIView* view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(10, 100, 300, 30);
    view2.backgroundColor = [UIColor blackColor];
    view2.tag = 2; // 唯一特征值,用于区分
    [view1 addSubview:view2];
    
    UIView* view3 = [[UIView alloc]init];
    view3.frame = CGRectMake(20, 50, 100, 100);
    view3.backgroundColor = [UIColor purpleColor];
    view3.tag = 3;
    
    [view1 addSubview:view3];
    
    // 子视图
    // 1.  通过遍历,比较tag获取特定子视图---比较麻烦
    NSArray* subViewArray = [view1 subviews];
    for(UIView* view in subViewArray){
        if(view.tag == 2){
            view.backgroundColor = [UIColor whiteColor];
        }
    }
    
    //2. 通过调用 viewWithTag 来获取
    UIView* subview = [view1 viewWithTag:3];
    subview.backgroundColor = [UIColor orangeColor];
    
    // 层级的处理
    // 1. 同一个父视图中的先加入的view 会被盖在下面
    // 2. 不同的父视图, 其子视图优先级和父视图挂钩
//    UIView* view4 = [[UIView alloc]init];
//    view4.frame = CGRectMake(100, 100, 300, 300);
//    view4.backgroundColor = [UIColor yellowColor];
//    [self.view addSubview:view4];
    // 3. 调整层级, 必须填写正确的层数
    [view1 exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    // 4. 插入指定层
    UIView* view5 = [[UIView alloc]init];
    view5.frame = CGRectMake(7, 80, 200, 200);
    view5.backgroundColor = [UIColor blackColor];
    [view1 insertSubview:view5 atIndex:1];
    NSLog(@"before");
    
//自适应
    UIView* backView = [[UIView alloc]init];
    backView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 25 , 400, 50, 50);
    backView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:backView];
    
    UIView* topView = [[UIView alloc]init];
    NSLog(@"到这里了");
    
    topView.frame = CGRectMake(10, 10, 30, 30);
    topView.backgroundColor = [UIColor blackColor];
    [backView addSubview:topView];
    NSArray* array3 = [backView subviews];
    if(array3.count > 0){
        NSLog(@"加上了");
    }
    else{
        NSLog(@"没加上");
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值