![](/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.