自学iOS开发系列----UI(视图编程入门:UIView)

本文详细介绍了UIView在iOS开发中的作用及其层次结构和事件处理机制。包括子视图的添加、显示顺序调整、子视图间的位置交换以及透明度设置等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇博客,教学目标:
UIView

UIView是UIKit中部分控件的直接或者间接的父类。

UIView的层次和事件接收
【1】父视图不能接收事件,则子视图无法接收事件
【2】子视图超出父视图部分,不能接收事件
【3】同一个父视图下,最上面的视图,首先遭遇事件,如果能够响应,就不向下传递事件,如果不能响应,事件向下传递.

1.在视图A上加载视图B,那么视图B就是视图A的子视图,视图A就是视图B的父视图。父视图可以有很多子视图,但是子视图只能有一个父视图

    CGFloat left = 20;
    CGFloat top = 30;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];

2.后添加的子视图,显示在最上面。多个子视图的frame如果有重合部分,我们不能通过视觉效果判断谁是谁的父视图,必须通过superview方法来判断父视图

    CGFloat left = 20;
    CGFloat top = 100;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.backgroundColor = [UIColor redColor];
    view1.tag = 101;
    [self.view addSubview:view1];

    UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];
    view2.backgroundColor = [UIColor blueColor];
    view2.tag = 102;
    [self.view addSubview:view2];

    UIView * superView1 = view1.superview;
    UIView * superView2 = view2.superview;
    if (superView1 == superView2) {
        NSLog(@"view1和view2是同一个父视图");
    }

3.在某个子视图上面插入一个子视图

    CGFloat left = 20;
    CGFloat top = 170;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];

    top = top+20;
    UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];
    view2.backgroundColor = [UIColor yellowColor];
    [self.view insertSubview:view2 aboveSubview:view1];

4.在某个子视图下面插入一个子视图

    CGFloat left = 20;
    CGFloat top = 260;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];

    top = top+20;
    UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];
    view2.backgroundColor = [UIColor greenColor];
    [self.view insertSubview:view2 belowSubview:view1];

5.交换两个子视图在父视图上的位置

    CGFloat left = 20;
    CGFloat top = 340;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];

    top = top+20;
    UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];
    view2.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view2];

    NSArray *subviews = [self.view subviews];
    NSInteger index1 = [subviews indexOfObject:view1];
    NSInteger index2 = [subviews indexOfObject:view2];
    [self.view exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];

6.将某个子视图提到所有子视图的最前面

    CGFloat left = 20;
    CGFloat top = 340;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];

    top = top+20;
    UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];
    view2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view2];

    top = top+20;
    UIView * view3 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];
    view3.backgroundColor = [UIColor brownColor];
    [self.view addSubview:view3];
    [self.view bringSubviewToFront:view2];

7.将某个子视图扔到所有子视图的最后面

    CGFloat left = 20;
    CGFloat top = 460;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];

    top = top+20;
    UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];
    view2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view2];

    top = top+20;
    UIView * view3 = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height)];
    view3.backgroundColor = [UIColor brownColor];
    [self.view addSubview:view3];
    [self.view sendSubviewToBack:view2];

8.从父视图上移除某个子视图

    CGFloat left = 20;
    CGFloat top = 560;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    [view1 removeFromSuperview];

9.设置视图透明度

CGFloat left = 20;
    CGFloat top = 615;
    CGFloat width = CGRectGetWidth(self.view.bounds)-2*left;
    CGFloat height = 50;

    UIView * view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(left, top, width, height);
    view1.alpha = 0.5;
    view1.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view1];

github地址:demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值