Bounds和Frame简介

Posted on 2016-12-04 20:10  柠檬片  阅读(67)  评论(0)    收藏  举报
#import "ViewController.h"

/*
    frame:以父控件左上角为原点
    bounds:以自己的左上角为原点,bounds x,y永远为0(🙅)
    
    frame和bounds都是用来描述一块区域
    frame:描述可视范围
 
    bounds:描述可视范围在内容的区域
    所有的子控件都是相对于内容
    bounds:修改内容原点
 
    相对性:可视范围相对于父控件位置永远不变
            可视范围相对于内容,位置改变
 
 */

@interface ViewController ()


@property (nonatomic, weak) UIView *redView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    redView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:redView];
    _redView = redView;
    
    UISwitch *switchView = [[UISwitch alloc] init];
    [_redView addSubview:switchView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGRect bounds = _redView.bounds;
    bounds.origin.y += 10;
    _redView.bounds = bounds;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end