fram和bounds的区别和用途 - iOS - UI基础知识总结2
2015-05-23 11:27
405 查看
// fram__________________________________
// 子视图的fram是相对于父视图的,改变fram的内容会改变子视图自己在父视图中的位置和大小,但是对父视图没有影响
// 设置fram时可以超出父视图边界范围(超出部分不会在父视图上展现)(比如:设置成(-10,-10,100,100))
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
redView.backgroundColor = [UIColor redColor];
[containerView addSubview:redView];
[redView release];
// 子视图的fram的起始位置(原点坐标)是针对父视图的 起始位置
// 父视图的fram的改变不会影响它的子视图在它上面的 相对位置
UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];// fram里面的原点(0,0)是相对于父视图的
blueView.backgroundColor = [UIColor blueColor];
[redView addSubview:blueView];
[blueView release];
// 当本身的fram和center发生冲突时,fram要遵从center的改变(改变center会引起fram跟着做出相应改变)
blueView.center = CGPointMake(50, 50);
NSLog(@"%f , %f",blueView.center.x , blueView.center.y);
NSLog(@"%@",blueView);
// bounds__________________________________
// bounds的改变对本身没有影响(自己相对于自己的父视图不会有偏移),但是会影响它上面的子视图的位置(它的子视图相对于它会有所偏移)
NSLog(@"bounds.x = %.2f , bounds.y = %.2f , bounds.width = %.2f , bounds.height = %.2f",redView.bounds.origin.x , redView.bounds.origin.y , redView.bounds.size.width , redView.bounds.size.height);
redView.bounds = CGRectMake(10, 10, 100, 100);
// 系统内部存放的是视图的中心点位置和大小信息,Frame方式的信息是按照中心点位置计算出来的。当我们创建一个视图的时,一般采用Frame方式。当我们旋转一个视图或者处理视图事件时,大多采用bounds方式(让其父视图的bounds改变,从而引起该视图在其父视图上的位置的改变)。
// 子视图的fram是相对于父视图的,改变fram的内容会改变子视图自己在父视图中的位置和大小,但是对父视图没有影响
// 设置fram时可以超出父视图边界范围(超出部分不会在父视图上展现)(比如:设置成(-10,-10,100,100))
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
redView.backgroundColor = [UIColor redColor];
[containerView addSubview:redView];
[redView release];
// 子视图的fram的起始位置(原点坐标)是针对父视图的 起始位置
// 父视图的fram的改变不会影响它的子视图在它上面的 相对位置
UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];// fram里面的原点(0,0)是相对于父视图的
blueView.backgroundColor = [UIColor blueColor];
[redView addSubview:blueView];
[blueView release];
// 当本身的fram和center发生冲突时,fram要遵从center的改变(改变center会引起fram跟着做出相应改变)
blueView.center = CGPointMake(50, 50);
NSLog(@"%f , %f",blueView.center.x , blueView.center.y);
NSLog(@"%@",blueView);
// bounds__________________________________
// bounds的改变对本身没有影响(自己相对于自己的父视图不会有偏移),但是会影响它上面的子视图的位置(它的子视图相对于它会有所偏移)
NSLog(@"bounds.x = %.2f , bounds.y = %.2f , bounds.width = %.2f , bounds.height = %.2f",redView.bounds.origin.x , redView.bounds.origin.y , redView.bounds.size.width , redView.bounds.size.height);
redView.bounds = CGRectMake(10, 10, 100, 100);
// 系统内部存放的是视图的中心点位置和大小信息,Frame方式的信息是按照中心点位置计算出来的。当我们创建一个视图的时,一般采用Frame方式。当我们旋转一个视图或者处理视图事件时,大多采用bounds方式(让其父视图的bounds改变,从而引起该视图在其父视图上的位置的改变)。
相关文章推荐
- UITextField - iOS - UI基础知识总结4
- iOS 基础知识总结 self.name = @"老王" 与 _name = @"老王" 的区别
- UIScrollView的基本使用方法和注意事项 - iOS - UI基础知识总结10
- UILable常见用法 - iOS - UI基础知识总结3
- iOS之UI基础---frame和bounds的区别
- UISlider - iOS - UI基础知识总结9
- UINavigationController - iOS - UI基础知识总结12
- UIButton - iOS - UI基础知识总结5
- UIGestureRecognizer 六种手势的用法 - iOS - UI基础知识总结6
- UIView的一般操作 - iOS - UI基础知识总结1
- 单例模式:单例类的标准写法 - iOS - UI基础知识总结13
- UITabBarController用法详解 - iOS - UI基础知识总结15
- UIImageView用法大全 - iOS - UI基础知识总结7
- UIScrollView和UIPageControl的配合使用 - iOS - UI基础知识总结11
- UISegmentedControl - iOS - UI基础知识总结8
- UIImagePickerController 的基本用法 - iOS - UI基础知识总结14
- iOS开发UI基础—手写控件,frame,center和bounds属性
- IOSUI基础控件总结
- IOS一句话总结基础知识
- 【iOS知识学习】_视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途