视图UIView的frame、bounds与center属性
2014-10-14 09:44
260 查看
UIView类中定义了三个属性,分别是frame、bounds与center属性:
[plain] view
plaincopy
@property(nonatomic) CGRect frame;
@property(nonatomic) CGRect bounds;
@property(nonatomic) CGPoint center;
frame属性没什么太特别的,它指的是视图在其父视图坐标系中的位置与尺寸。在创建视图时,我们就需要在初始化方法中指定视图的框架尺寸:
[plain] view
plaincopy
- (id)initWithFrame:(CGRect)frame;
这样创建了视图之后,通过addSubview:方法就可以将创建的子视图放置在父视图的指定位置,并以指定的尺寸显示出来。
center属性理解起来也很简单,它就是视图的中心点在其父视图坐标系中的位置坐标。简单讲,center属性就定义了当前视图在父视图中的位置。
那么bounds属性呢?它指的是视图在其自己的坐标系中的位置与尺寸。由于视图的定位一定需要针对父视图,而不是针对自己,所以bounds属性并不能够决定当前视图的位置(因为它与父视图根本无关,就谈不上定位),所以bounds属性与center属性是完全独立的,前者规定尺寸,后者定义位置。换句话说,修改bounds不影响center;修改center不影响bounds。(《iOS应用程序开发方法与实践》中讲到当修改bounds时center会更改,这是一个错误。特附此文。)
默认情况下,当视图创建后,其自己的坐标系原点(0, 0)位于其左上角位置。不过,本地原点位置是会随时发生变化的。例如将bounds由最初的(0, 0, 100, 100)修改为(50, 50, 100, 100),则意味着本地原点向左向上移动了50。由于位置和尺寸都未发生变化,所以frame属性与center属性不变。
[plain] view
plaincopy
UIView* testView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
[self.window addSubview:testView];
NSLog(@"Frame: (%.2f, %.2f, %.2f, %.2f)", testView.frame.origin.x, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height);
NSLog(@"Bounds: (%.2f, %.2f, %.2f, %.2f)", testView.bounds.origin.x, testView.bounds.origin.y, testView.bounds.size.width, testView.bounds.size.height);
NSLog(@"Center: (%.2f, %.2f)", testView.center.x, testView.center.y);
NSLog(@"Modify Bounds to (50, 50, 100, 100)");
testView.bounds = CGRectMake(50, 50, 100, 100);
NSLog(@"Frame: (%.2f, %.2f, %.2f, %.2f)", testView.frame.origin.x, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height);
NSLog(@"Bounds: (%.2f, %.2f, %.2f, %.2f)", testView.bounds.origin.x, testView.bounds.origin.y, testView.bounds.size.width, testView.bounds.size.height);
NSLog(@"Center: (%.2f, %.2f)", testView.center.x, testView.center.y);
输出:
[plain] view
plaincopy
Frame: (50.00, 50.00, 100.00, 100.00)
Bounds: (0.00, 0.00, 100.00, 100.00)
Center: (100.00, 100.00)
Modify Bounds to (50, 50, 100, 100)
Frame: (50.00, 50.00, 100.00, 100.00)
Bounds: (50.00, 50.00, 100.00, 100.00)
Center: (100.00, 100.00)
再例如将bounds由最初的(0, 0, 100, 100)修改为(0, 0, 140, 140),则本地原点向左向上移动20。frame属性变为(30, 30, 140, 140),center属性不变,仍然是(100, 100)。
[plain] view
plaincopy
UIView* testView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
[self.window addSubview:testView];
NSLog(@"Frame: (%.2f, %.2f, %.2f, %.2f)", testView.frame.origin.x, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height);
NSLog(@"Bounds: (%.2f, %.2f, %.2f, %.2f)", testView.bounds.origin.x, testView.bounds.origin.y, testView.bounds.size.width, testView.bounds.size.height);
NSLog(@"Center: (%.2f, %.2f)", testView.center.x, testView.center.y);
NSLog(@"Modify Bounds to (0, 0, 140, 140)");
testView.bounds = CGRectMake(0, 0, 140, 140);
NSLog(@"Frame: (%.2f, %.2f, %.2f, %.2f)", testView.frame.origin.x, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height);
NSLog(@"Bounds: (%.2f, %.2f, %.2f, %.2f)", testView.bounds.origin.x, testView.bounds.origin.y, testView.bounds.size.width, testView.bounds.size.height);
NSLog(@"Center: (%.2f, %.2f)", testView.center.x, testView.center.y);
输出:
[plain] view
plaincopy
Frame: (50.00, 50.00, 100.00, 100.00)
Bounds: (0.00, 0.00, 100.00, 100.00)
Center: (100.00, 100.00)
Modify Bounds to (0, 0, 140, 140)
Frame: (30.00, 30.00, 140.00, 140.00)
Bounds: (0.00, 0.00, 140.00, 140.00)
Center: (100.00, 100.00)
[plain] view
plaincopy
@property(nonatomic) CGRect frame;
@property(nonatomic) CGRect bounds;
@property(nonatomic) CGPoint center;
frame属性没什么太特别的,它指的是视图在其父视图坐标系中的位置与尺寸。在创建视图时,我们就需要在初始化方法中指定视图的框架尺寸:
[plain] view
plaincopy
- (id)initWithFrame:(CGRect)frame;
这样创建了视图之后,通过addSubview:方法就可以将创建的子视图放置在父视图的指定位置,并以指定的尺寸显示出来。
center属性理解起来也很简单,它就是视图的中心点在其父视图坐标系中的位置坐标。简单讲,center属性就定义了当前视图在父视图中的位置。
那么bounds属性呢?它指的是视图在其自己的坐标系中的位置与尺寸。由于视图的定位一定需要针对父视图,而不是针对自己,所以bounds属性并不能够决定当前视图的位置(因为它与父视图根本无关,就谈不上定位),所以bounds属性与center属性是完全独立的,前者规定尺寸,后者定义位置。换句话说,修改bounds不影响center;修改center不影响bounds。(《iOS应用程序开发方法与实践》中讲到当修改bounds时center会更改,这是一个错误。特附此文。)
默认情况下,当视图创建后,其自己的坐标系原点(0, 0)位于其左上角位置。不过,本地原点位置是会随时发生变化的。例如将bounds由最初的(0, 0, 100, 100)修改为(50, 50, 100, 100),则意味着本地原点向左向上移动了50。由于位置和尺寸都未发生变化,所以frame属性与center属性不变。
[plain] view
plaincopy
UIView* testView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
[self.window addSubview:testView];
NSLog(@"Frame: (%.2f, %.2f, %.2f, %.2f)", testView.frame.origin.x, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height);
NSLog(@"Bounds: (%.2f, %.2f, %.2f, %.2f)", testView.bounds.origin.x, testView.bounds.origin.y, testView.bounds.size.width, testView.bounds.size.height);
NSLog(@"Center: (%.2f, %.2f)", testView.center.x, testView.center.y);
NSLog(@"Modify Bounds to (50, 50, 100, 100)");
testView.bounds = CGRectMake(50, 50, 100, 100);
NSLog(@"Frame: (%.2f, %.2f, %.2f, %.2f)", testView.frame.origin.x, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height);
NSLog(@"Bounds: (%.2f, %.2f, %.2f, %.2f)", testView.bounds.origin.x, testView.bounds.origin.y, testView.bounds.size.width, testView.bounds.size.height);
NSLog(@"Center: (%.2f, %.2f)", testView.center.x, testView.center.y);
输出:
[plain] view
plaincopy
Frame: (50.00, 50.00, 100.00, 100.00)
Bounds: (0.00, 0.00, 100.00, 100.00)
Center: (100.00, 100.00)
Modify Bounds to (50, 50, 100, 100)
Frame: (50.00, 50.00, 100.00, 100.00)
Bounds: (50.00, 50.00, 100.00, 100.00)
Center: (100.00, 100.00)
再例如将bounds由最初的(0, 0, 100, 100)修改为(0, 0, 140, 140),则本地原点向左向上移动20。frame属性变为(30, 30, 140, 140),center属性不变,仍然是(100, 100)。
[plain] view
plaincopy
UIView* testView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
[self.window addSubview:testView];
NSLog(@"Frame: (%.2f, %.2f, %.2f, %.2f)", testView.frame.origin.x, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height);
NSLog(@"Bounds: (%.2f, %.2f, %.2f, %.2f)", testView.bounds.origin.x, testView.bounds.origin.y, testView.bounds.size.width, testView.bounds.size.height);
NSLog(@"Center: (%.2f, %.2f)", testView.center.x, testView.center.y);
NSLog(@"Modify Bounds to (0, 0, 140, 140)");
testView.bounds = CGRectMake(0, 0, 140, 140);
NSLog(@"Frame: (%.2f, %.2f, %.2f, %.2f)", testView.frame.origin.x, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height);
NSLog(@"Bounds: (%.2f, %.2f, %.2f, %.2f)", testView.bounds.origin.x, testView.bounds.origin.y, testView.bounds.size.width, testView.bounds.size.height);
NSLog(@"Center: (%.2f, %.2f)", testView.center.x, testView.center.y);
输出:
[plain] view
plaincopy
Frame: (50.00, 50.00, 100.00, 100.00)
Bounds: (0.00, 0.00, 100.00, 100.00)
Center: (100.00, 100.00)
Modify Bounds to (0, 0, 140, 140)
Frame: (30.00, 30.00, 140.00, 140.00)
Bounds: (0.00, 0.00, 140.00, 140.00)
Center: (100.00, 100.00)
相关文章推荐
- 视图UIView的frame、bounds与center属性
- 视图UIView的frame、bounds与center属性
- 视图UIView的frame、bounds与center属性
- 视图UIView的frame、bounds与center属性
- UIView 简介(六)视图几何结构和坐标系统 包括frame, bounds和center属性之间的关系
- 详解UIView的frame、bounds和center属性
- UI基础--UIView常见属性之frame、center、bounds、transframe属性
- UIView 中 frame, bounds, center 属性的关系
- iOS--------坐标系统(UIView的frame、bounds跟center属性)
- iOS之 view视图的创建和它的Frame、bounds、center属性
- 详解UIView的frame、bounds和center属性
- 详解UIView的frame、bounds、center属性
- 详解UIView的frame、bounds和center属性
- ios-坐标系统(详解UIView的frame、bounds跟center属性[图])
- Ios-坐标系统(详解UIView的frame、bounds跟center属性[图])
- ios-坐标系统(详解UIView的frame、bounds跟center属性[图])
- 详解UIView的frame、bounds和center属性
- ios-坐标系统(详解UIView的frame、bounds跟center属性[图])
- ios开发之UIView的frame、bounds跟center属性的区别(附图)
- ios-坐标系统(详解UIView的frame、bounds跟center属性[图])