frame 与 bounds的区别

/*  打印相关struct
 NSStringFromCGPoint
 NSStringFromCGSize
 NSStringFromCGRect
 NSStringFromCGAffineTransform
 NSStringFromUIEdgeInsets
 */
- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
	UIView *fatherV = [UIView new];
	fatherV.frame = CGRectMake(50, 50, 200, 300);
	fatherV.backgroundColor = [UIColor blueColor];
	[self.view addSubview:fatherV];
	[fatherV setBounds:CGRectMake(50, 50, 200, 300)];
	NSLog(@"father.frame ===%@",NSStringFromCGRect(fatherV.frame));
	NSLog(@"father.bounds===%@",NSStringFromCGRect(fatherV.bounds));
	
	
	UIView *sonView = [UIView new];
	sonView.frame = CGRectMake(0, 0, 100, 100);
	sonView.backgroundColor = [UIColor redColor];
	[fatherV addSubview:sonView];
	NSLog(@"sonView.frame ===%@",NSStringFromCGRect(sonView.frame));//坐标根据fatherV.frame
	NSLog(@"sonView.bounds===%@",NSStringFromCGRect(sonView.bounds));

}

输入图片说明

//log
 father.frame ==={{50, 50}, {200, 300}}
 father.bounds==={{0, 0}, {200, 300}}
 sonView.frame ==={{0, 0}, {100, 100}}
 sonView.bounds==={{0, 0}, {100, 100}}

输入图片说明

//log
father.frame ==={{50, 50}, {200, 300}}
father.bounds==={{-50, -50}, {200, 300}}
sonView.frame ==={{0, 0}, {100, 100}}
 sonView.bounds==={{0, 0}, {100, 100}}

此时把father.bounds设为了{-50, -50},导致sonView所在的原点坐标为{-50, -50},设置sonView.frame{0, 0}时,便会向上图坐标平移

参考文章: http://blog.qiji.tech/archives/13019, https://www.jianshu.com/p/964313cfbdaa

猜你喜欢

转载自my.oschina.net/u/2319073/blog/1630489