I need to programmatically do the following. How can I do this? The code should be dynamic enough to fit a portrait view inside landscape also. The views are not UIImageViews.
If you don't mind including the AVFoundation
framework in your project, you can use: AVMakeRectWithAspectRatioInsideRect(CGSize aspectRatio, CGRect boundingRect);
Its documentation states:
Returns a scaled CGRect that maintains the aspect ratio specified by a CGSize within a bounding CGRect.
It can be used as:
subview.frame = AVMakeRectWithAspectRatioInsideRect(subview.bounds.size, superview.bounds);
Parameters:
aspectRatio : The width and height ratio (aspect ratio) you want to maintain.
boundingRect : The bounding rectangle you want to fit into.
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)];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.