frame和bounds
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了frame和bounds相关的知识,希望对你有一定的参考价值。
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];
// 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.
}
[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.
}
// 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的主要内容,如果未能解决你的问题,请参考以下文章
iOS frame和Bounds 以及frame和bounds区别
ios view的frame和bounds之区别(位置和大小)