bounds和frame的区别

本文详细解释了在iOS开发中如何使用不同方式添加视图,并对比了frame与bounds的区别及其对视图位置的影响。

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

1.在view1视图上以view1.frame的格式添加view2视图


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

    

    view1.backgroundColor = [UIColor greenColor];

    

    [self.window addSubview:view1];

    

    

    UIView *view2 = [[UIView alloc] initWithFrame:view1.frame];

    

    view2.backgroundColor = [UIColor redColor];

    

    [view1 addSubview:view2];


解答:

①、view1的是将手机屏幕的左上角作为原点

②、view2则是将view1的左上角作为原点

③、可以理解为:view2以view1作为self.window,然后添加(view1就是view2的父视图)

④、在A视图上添加B视图,如果B以A的frame格式添加视图,就是以A作为self.window





2、在view1视图上一view1.brounds的格式添加视图


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

    view1.backgroundColor = [UIColor greenColor];

    [self.window addSubview:view1];

    

    UIView *view2 = [[UIView alloc] initWithFrame:view1.bounds];

    view2.backgroundColor = [UIColor redColor];

    [view1 addSubview:view2];


解答:

①、此时view1和view2都是以屏幕的左上角作为原点

②、通俗点说就是view2是在view1视图上拷贝了view1,然后再view1所在的位置上黏贴了view2

③、个人认为view2将self.window的原点,添加

④、在A视图上添加B视图,若B视图是以A视图的bounds格式添加,此时B视图以self.window的原点,添加



    



3、在self.window视图上以view1.bounds格式添加view2


    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 0, 200, 200)];

    view1.backgroundColor = [UIColor greenColor];

    [self.window addSubview:view1];

    

    UIView *view2 = [[UIView alloc] initWithFrame:view1.bounds];

    view2.backgroundColor = [UIColor redColor];

    [self.window addSubview:view2];

    

    view2.alpha = 0.3;


解答:

①、此时的屏幕原本的原点有(0,0),改成了(-50,0);

②、也可以说是将view1的x坐标和y坐标取相反数作为self.window的原点

③、在self.window视图上,如果B视图以A视图的bounds格式添加,此时的self.window取A视图的X坐标和Y坐标的各自相反数作为自己的原点



4、在self.widow视图上一view1.frame格式添加view2

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 0, 200, 200)];

    view1.backgroundColor = [UIColor greenColor];

    [self.window addSubview:view1];

    

    

    UIView *view2 = [[UIView alloc] initWithFrame:view1.frame];

    view2.backgroundColor = [UIColor redColor];

    [self.window addSubview:view2];


    view2.alpha = 0.3;

解答:

①、以view1的创建格式做为view2的格式添加view2

②、通俗说就是view2在view1的位置上拷贝了view1

③、在self.window视图上,以A视图的frame格式添加B视图,就是B视图在A视图的位置上拷贝了A视图且粘贴在A视图上




注:新手理解,高手请唔喷...







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值