簡體   English   中英

如何以編程方式設置子視圖的框架以使其適合其父視圖?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM