15.UIView

作者: Maserati丶 | 来源:发表于2019-02-18 17:28 被阅读0次

    课程来自慕课网Visitor.zc老师


    创建UIView
        // UIView view1
        UIView *view1 = [[UIView alloc] init];
        view1.frame = CGRectMake(10, 54, 350, 712);
        view1.backgroundColor = [UIColor redColor];
        [self.view addSubview:view1];
        
        NSLog(@"w:%f h:%f",[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height);
    
        
        // UIView view2
        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 *view3 = [[UIView alloc] init];
        view3.frame = CGRectMake(20, 50, 100, 100);
        view3.backgroundColor = [UIColor purpleColor];
        view3.tag = 3;  // 唯一标识
        [view1 addSubview:view3];
    
    父视图与子视图
        // superview
        UIView *superview = view1.superview;
        superview.backgroundColor = [UIColor grayColor];
        
        // subviews
        NSArray *subviewsArray = view1.subviews;
        for (UIView *view in subviewsArray) {
            if (view.tag == 2) {
                view.backgroundColor = [UIColor whiteColor];
            }
        }
        
        UIView *subView = [view1 viewWithTag:3];
        subView.backgroundColor = [UIColor blueColor];
    
    层级的处理
    1. 同一个父视图中后加入的View在上方
    2. 子视图跟随父视图进行层级遮挡
        // 交换两个层的视图
        [view1 exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
        // 插入一个视图到指定层
        UIView *view5 = [[UIView alloc] init];
        view5.frame = CGRectMake(7, 80, 200, 200);
        view5.backgroundColor = [UIColor greenColor];
        [view1 insertSubview:view5 atIndex:1];
        // 将一个View放入最上层或最下层
        [view1 bringSubviewToFront:view3];
        [view1 sendSubviewToBack:view3];
    
    自适应
        // 自适应
        UIView *backView = [[UIView alloc] init];
        backView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-25, 400, 50, 50);
        backView.backgroundColor = [UIColor yellowColor];
        backView.autoresizesSubviews = YES;  // 允许子视图自适应
        backView.tag = 1001;
        [self.view addSubview:backView];
        
        UIView *topView = [[UIView alloc] init];
        topView.frame = CGRectMake(10, 10, 30, 30);
        topView.backgroundColor = [UIColor purpleColor];
    //    topView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        topView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        [backView addSubview:topView];
    

    相关文章

      网友评论

          本文标题:15.UIView

          本文链接:https://www.haomeiwen.com/subject/sxkzeqtx.html