![](/img/trans.png)
[英]How to add a subview with constraints to make the subview with the same frame as its superview?
[英]How to programmatically set a subview's frame to fit it inside its superview?
我需要以編程方式執行以下操作。 我怎樣才能做到這一點? 該代碼應具有足夠的動態性,以適合橫向內的縱向視圖。 這些視圖不是UIImageViews。
如果您不介意在項目中包括AVFoundation
框架,則可以使用: AVMakeRectWithAspectRatioInsideRect(CGSize aspectRatio, CGRect boundingRect);
其文檔指出:
返回縮放的CGRect,該縮放的CGRect在邊界CGRect中保持CGSize指定的寬高比。
它可以用作:
subview.frame = AVMakeRectWithAspectRatioInsideRect(subview.bounds.size, superview.bounds);
參數:
AspectRatio :您要保持的寬高比(縱橫比)。
boundingRect :您要適合的邊界矩形。
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)];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.