码迷,mamicode.com
首页 > 移动开发 > 详细

ios视图frame和bounds的对比

时间:2014-07-01 20:02:59      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   strong   os   

bounds坐标:自己定义的坐标系统,setbound指明了本视图左上角在该坐标系统中的坐标,

        默认值(0,0)

frame坐标:  子视图左上角在父视图坐标系统(bounds坐标系统)中的坐标,默认值(0,0)

子视图实际位置=父视图实际位置-父视图bounds坐标+子视图frame坐标

一、bounds

  只影响“子视图”相对屏幕的位置,修改时不会影响自身相对屏幕的位置

  1、父视图bounds坐标为(0,0)时

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
}

  bubuko.com,布布扣

  2、父视图bounds坐标为(-20,-20)时 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    [self.view setBounds:CGRectMake(-20, -20, 320, 568)];
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
}

  bubuko.com,布布扣

二、frame

  修改时改变了自己的在父视图坐标系统(bounds坐标系统)的位置,自身位置和

  子视图位置都会被改变。

  1、父视图frame坐标(0,0)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.backgroundColor = [UIColor yellowColor];
    [view1 addSubview:view2];
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));
}

  bubuko.com,布布扣

  2、父视图frame坐标(60,60)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(60, 60, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.backgroundColor = [UIColor yellowColor];
    [view1 addSubview:view2];
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));
}

  bubuko.com,布布扣

三、说明

  根视图只能修改bounds坐标,而不可以修改frame坐标

  以下self.view为根视图

  1、初始状态

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
}

  bubuko.com,布布扣

  2、修改bounds坐标:有效

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    CGRect viewBounds = self.view.bounds;
    viewBounds.origin.y=-40;
    viewBounds.origin.x=-40;
    self.view.bounds=viewBounds;
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
}

   bubuko.com,布布扣

   bubuko.com,布布扣

  3、修改frame坐标:无效

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y=60;
    viewFrame.origin.x=60;
    self.view.frame=viewFrame;
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
}

  bubuko.com,布布扣

  bubuko.com,布布扣

ios视图frame和bounds的对比,布布扣,bubuko.com

ios视图frame和bounds的对比

标签:des   style   blog   color   strong   os   

原文地址:http://www.cnblogs.com/edisonfeng/p/3817034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!