iOS项目开发实战——理解frame,bounds,center
2015-09-25 11:26
465 查看
在iOS界面设计中,设置控件的大小往往会用到frame,bounds或者center。我们也同时可以来获得控件的位置信息。具体代码如下:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//视图;
UIView *view1 = [[UIView alloc] init];
//位置大小;
view1.frame = CGRectMake(20, 30, 100, 200);
//背景颜色;
view1.backgroundColor = [UIColor yellowColor];
//将视图加入到父视图中;
[self.view addSubview:view1];
//frame是实际视图位置与大小,是相对父视图而言的;
CGFloat x = view1.frame.origin.x;//获取view的横坐标;
CGFloat y = view1.frame.origin.y;//获取view的纵坐标;
CGFloat width = view1.frame.size.width;//获取view的宽度;
CGFloat height = view1.frame.size.height;//获取view的高度;
//bounds是View边框的位置与大小;
CGFloat xBounds = view1.bounds.origin.x;//获取view的横坐标;
CGFloat yBounds = view1.bounds.origin.y;//获取view的纵坐标;
CGFloat widthBounds = view1.bounds.size.width;//获取view的宽度;
CGFloat heightBounds = view1.bounds.size.height;//获取view的高度;
//center 中心点;
CGFloat xCenter = view1.center.x;
CGFloat yCenter = view1.center.y;
NSLog(@"frame x=%.0f y=%.0f width=%.0f height=%.0f",x,y,width,height);
NSLog(@"bounds x=%.0f y=%.0f width=%.0f height=%.0f",xBounds,yBounds,widthBounds,heightBounds);
NSLog(@"center x=%0.f y=%0.f",xCenter,yCenter);
}
@end
输出结果如下:
·
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//视图;
UIView *view1 = [[UIView alloc] init];
//位置大小;
view1.frame = CGRectMake(20, 30, 100, 200);
//背景颜色;
view1.backgroundColor = [UIColor yellowColor];
//将视图加入到父视图中;
[self.view addSubview:view1];
//frame是实际视图位置与大小,是相对父视图而言的;
CGFloat x = view1.frame.origin.x;//获取view的横坐标;
CGFloat y = view1.frame.origin.y;//获取view的纵坐标;
CGFloat width = view1.frame.size.width;//获取view的宽度;
CGFloat height = view1.frame.size.height;//获取view的高度;
//bounds是View边框的位置与大小;
CGFloat xBounds = view1.bounds.origin.x;//获取view的横坐标;
CGFloat yBounds = view1.bounds.origin.y;//获取view的纵坐标;
CGFloat widthBounds = view1.bounds.size.width;//获取view的宽度;
CGFloat heightBounds = view1.bounds.size.height;//获取view的高度;
//center 中心点;
CGFloat xCenter = view1.center.x;
CGFloat yCenter = view1.center.y;
NSLog(@"frame x=%.0f y=%.0f width=%.0f height=%.0f",x,y,width,height);
NSLog(@"bounds x=%.0f y=%.0f width=%.0f height=%.0f",xBounds,yBounds,widthBounds,heightBounds);
NSLog(@"center x=%0.f y=%0.f",xCenter,yCenter);
}
@end
输出结果如下:
·
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
相关文章推荐
- ios 随记
- iOS 从工程中删除Cocoapods
- ios学习笔记——对象归档
- iOS 开发该选择Blocks还是Delegates
- IOS里面的字典转模型
- iOS AutoLayout 自动布局
- iOS如何改变图片的尺寸?
- iOS多线程学习---控制线程通信及线程使用的总结
- iOS - NSLog的使用方法
- Xcode7 ios9 升级后出现的问题
- 一篇关于IOS适配很好的文章
- iOS项目开发实战——使用代码获取屏幕宽高
- iOS 中背景图片的设置
- IOS9 适配
- iOS AutoLayout 代码布局自动化
- iOS 之@()
- iOS开发~CocoaPods使用详细说明
- ios学习笔记——属性列表
- 开始iOS 7中自动布局教程(二)
- 开始iOS 7中自动布局教程(一)