三言两语frame&bounds
来源:互联网 发布:淘宝怎么账号登陆 编辑:程序博客网 时间:2024/06/07 05:12
简单说明一下ios view的frame属性和bounds属性,直接看代码。
- (void)viewDidLoad { [super viewDidLoad]; UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; centerView.backgroundColor = [UIColor redColor]; centerView.center = self.view.center; [self.view addSubview:centerView];// centerView.bounds = CGRectMake(0, 0, 200, 200); UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; subView.backgroundColor = [UIColor blackColor]; [centerView addSubview:subView];}效果:如果我们修改view的bounds属性:- (void)viewDidLoad { [super viewDidLoad]; UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; centerView.backgroundColor = [UIColor redColor]; centerView.center = self.view.center; [self.view addSubview:centerView]; //修改centerView的bounds属性 centerView.bounds = CGRectMake(10, 10, 300, 400); UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; subView.backgroundColor = [UIColor blackColor]; [centerView addSubview:subView];}
效果:我们可以看到,红色的centerView变大了,但是相对于父视图的位置没有发生变化,因为我们没有改变他的frame属性,黑色的subView大小没有变化,但是相对于centerView的位置发生了变化。
解释:
frame属性中的(x, y, width, height),x和y都是说这个视图相对于父视图的位置,后者表示本身的大小;
bounds属性中的x和y,表示的是这个视图本身所拥有的坐标系统的原点位置,默认在视图的左上角,即(0, 0),我们修改为(10, 10),这时候会把他的自身坐标系统像左上方移动10,但是视图本身的位置并不会发生变化,仅仅是它所拥有的坐标系统原点发生了变化,因为很色subView是参照这个坐标系的,因此黑色的subView的原点相对红色视图左上角的点,偏移了(10, 10)
1 0
- 三言两语frame&bounds
- frame bounds
- bounds和frame区别
- Bounds,frame in IOS
- frame bounds contentOffset contentInset
- ios bounds 和 frame
- frame、bounds、center
- frame.bounds和center
- addSubview,frame和bounds
- IOS bounds与Frame
- frame、bounds区别
- bounds和frame区别
- bounds和frame区别
- frame,bounds的区别
- bounds 和frame区别
- frame 和 bounds 区别
- frame vs bounds
- UI UIWindow / bounds / frame
- python读取中文文件乱码
- openOffice集成java中,用EXT展示
- android ARM 汇编学习 —— hello world
- 一个程序员的Java和C++学习之路(整理)
- 拉格朗日插值法
- 三言两语frame&bounds
- 万物作焉而不辞
- python 八荣八耻
- linux下python链接mongodb
- php递归使用示例(php递归函数)
- 如何搞定你的直接上司
- 根据“年月日”字符串,判断这一天是星期几
- 算法的时间复杂度和空间复杂度
- 【存储过程】利用包构建存储过程和存储函数的案例(转)