您的位置:首页 > 移动开发 > IOS开发

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  。欢迎大家访问!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: