- 一个控件的bounds默认是(0,0,宽,高)。
- 一个控件的frame就是以父控件的bounds的原点为原点的位置。
- 修改自己的bounds值,bounds原点被改变了,所以子控件的位置会改变。
- 子控件相对于父控件位置是frame,父控件的bounds改变可以相对改变子控件的位置。
- 修改frame相当于修改自己的位置,修改bounds就相当于改了子控件的坐标系。
如下代码,每次点击self.view的时候,switchView就会上移:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) UIView *_blueView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
_blueView = blueView;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[_blueView addSubview:switchView];
}
//每次点击的时候switchView就会向上移动10
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CGRect bounds = _blueView.bounds;
bounds.origin.y += 10;
_blueView.bounds = bounds;
}
@end
如下图: