美文网首页
iOS view的frame和bounds之区别

iOS view的frame和bounds之区别

作者: 文森star | 来源:发表于2017-07-03 23:18 被阅读0次

一、首先列一下公认的资料:

先看到下面的代码你肯定就明白了一些:

-(CGRect)frame{

return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);

}

-(CGRect)bounds{

return CGRectMake(0,0,self.frame.size.width,self.frame.size.height);

}

很明显,bounds的原点是(0,0)点(就是view本身的坐标系统,默认永远都是0,0点,除非认为setbounds),而frame的原点却是任意的(相对于父视图中的坐标位置)。

再来看张图就明白了,

frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)

bounds:该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统,就相当于ViewB自己的坐标系统,以0,0点为起点)

center:该view的中心点在父view坐标系统中的位置和大小。(参照电是,父亲的坐标系统)

我个人认为,bounds稍微有点费解,稍不留神,想的多了,就会绕进去。每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触 摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地 坐标系统的关键就是要知道的它的原点(0,0)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是 window它的本地坐标系统原点就是屏幕的左上角了)。通过修改view的bounds属性可以修改本地坐标系统的原点位置。

所以,我个人认为,bounds影响到子view的位置和大小。

二、demo演示:

[cpp]view plaincopy

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 250)];

[view1 setBounds:CGRectMake(-20, -20, 280, 250)];

view1.backgroundColor = [UIColor redColor];

[self.view addSubview:view1];//添加到self.view

NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));

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

view2.backgroundColor = [UIColor yellowColor];

[view1 addSubview:view2];//添加到view1上,[此时view1坐标系左上角起点为(-20,-20)]

NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));

view1的位置,由view1.frame来决定,

view1.bounds决定的是其内子视图的原点.

既然原点在-20,-20处,那么0,0当然就下移了

(log输出日志表明,每个新的view默认的bounds其实都是(0,0))

相关文章

  • iOS纪录

    (1)View的Frame与Bounds区别 摘自 ios view的frame和bounds之区别(位置和大小)...

  • 【技巧】View的Frame与Bounds区别

    摘自 ios view的frame和bounds之区别(位置和大小) 事例代码 下图说明一切~

  • iOS 面试题目

    1、iOS frame和Bounds 以及frame和bounds区别2、 ios webView 加载HTML字...

  • 考试题

    一、 1.frame与bounds的区别: (1) Frame: frame的view是在父视图的view坐标...

  • iOS view的frame和bounds之区别

    一、首先列一下公认的资料: 先看到下面的代码你肯定就明白了一些: -(CGRect)frame{ return C...

  • 面试题总结

    1、Frame和Bounds的区别? (图片摘抄网络,侵权删) frame: 该view在父view坐标系统中的位...

  • Swift学习总结1

    1.bounds 与 frame的区别: frame: view在父view坐标系统中的位置和大小。(参照点是,父...

  • size和center、frame和bounds

    frame和bounds的区别 frame指的是:该view在父view坐标系统中的位置和大小。(参照点是父控件的...

  • View的frame和bounds之区别

    先看到下面的代码你肯定就明白了一些: -(CGRect)frame{return CGRectMake(self....

  • iOS View的Frame和bounds之区别,setboun

    简单理解就是Frame 参照父布局的位置,bounds参照自己 https://blog.csdn.net/hhe...

网友评论

      本文标题:iOS view的frame和bounds之区别

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