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}時,便會向上圖座標平移code

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

相關文章
相關標籤/搜索