2014-09-12 64 views

回答

3

如果你不介意的話,包括在項目中AVFoundation框架,你可以使用:AVMakeRectWithAspectRatioInsideRect(CGSize aspectRatio, CGRect boundingRect);

其文檔狀態:

返回縮放的CGRect維持由指定的長寬比邊界CGRect中的CGSize。

它可以用作:

subview.frame = AVMakeRectWithAspectRatioInsideRect(subview.bounds.size, superview.bounds); 

參數:

的aspectRatio:寬度和高度之比(縱橫比)要保持。

boundingRect:您想要適合的邊界矩形。

+0

包含AVFoundation框架的缺點是什麼?一些性能問題或.. – 2016-03-11 21:48:09

+0

不,真的不應該有任何缺點@NerminSehic – Wain 2016-03-11 21:59:12

0
float ratio = subView.bounds.size.height/subView.bounds.size.width; 

[subView setFrame:CGRectMake(0,0,superView.bounds.size.width,superView.bounds.size.width*ratio)]; 
[subView setCenter:CGPointMake(superView.bounds.size.width/2,superView.bounds.size.height/2)]; 
+0

我喜歡你集中視圖的方式。但是你設置框架的方式不是動態的,儘管它可以用於這個例子。 – 2014-09-12 13:16:27

相關問題