首页 > 其他 > 详细

frame和bounds

时间:2016-02-16 18:26:52      阅读:175      评论:0      收藏:0      [点我收藏+]
frame其实也是一个结构体,是CGRect的一个变量。
//
//  ViewController.m
//  UIView01
//
//  Created by cqy on 16/2/12.
//  Copyright © 2016年 程清杨. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建View对象
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 100)];
    //设置背景颜色
    view1.backgroundColor = [UIColor yellowColor];
    //添加视图
    [self.view addSubview:view1];
    //frame其实也是一个结构体,有两个成员变量均为结构体,两个成员变量分别有两个成员变量都是cgfloat类型的;
    UIView *view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(20, 140, 200, 100);
    view2.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view2];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
bounds:在每添加?个view的时候,每个view分别以??的起点为(0,0)点,创建?个坐标系,这个bounds就是?个结构体,?来确定view的新坐标。 
//  ViewController.m
//  UIView01
//
//  Created by cqy on 16/2/12.
//  Copyright © 2016年 程清杨. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建View对象
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 100)];
    //设置背景颜色
    view1.backgroundColor = [UIColor yellowColor];
    //添加视图
    [self.view addSubview:view1];
    //frame其实也是一个结构体,有两个成员变量均为结构体,两个成员变量分别有两个成员变量都是cgfloat类型的;
    UIView *view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(20, 140, 200, 100);
    view2.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view2];
    //bounds也是结构体,与frame一样。只是坐标是从原点开始;
    UIView *view3 = [[UIView alloc]init];
    view3.bounds = CGRectMake(0, 0, 50, 50);
    view3.backgroundColor = [UIColor redColor];
    [view2 addSubview:view3];
    NSLog(@"view1.bounds:%@",NSStringFromCGRect(view1.bounds));//结果:view1.bounds:{{0, 0}, {200, 100}}
    NSLog(@"view1.frame:%@",NSStringFromCGRect(view1.frame));//结果: view1.frame:{{20, 20}, {200, 100}}
    //center是以视图坐标系为参照,取得中点
    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(20, 260, 200, 100)];
    view4.backgroundColor = [UIColor brownColor];
    [self.view addSubview:view4];
     NSLog(@"view1.center:%@",NSStringFromCGPoint(view1.center));//结果:view1.center:{120, 70}
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
 
frame与bounds的区别:frame与bounds都是结构体,都是?来确定位置和??的;
frame的位置是从屏幕的最左上?开始算的;    
bounds是从新添加的view的最左上?的点开始算。

frame和bounds

原文:http://www.cnblogs.com/iQingYang/p/5193167.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!