I have recently discovered this method through a work colleague. I have been manually computing the size that fits a CGRect but maintains aspect ratio before, but this method will easily replace that. This is one of the benefits of working with new iOS developers every now and then; you get to discover new things that will make development easier.

Note that the center of the CGRect returned by this function is the same as the center of the boundingRect passed in the function.

AVMakeRectWithAspectRatioInsideRect image


Use Cases

According to Apple Documentation:

This is useful when attempting to fit the presentationSize property of an AVPlayerItem within the bounds of another CALayer. You would typically use the return value of this function as an AVPlayerLayer frame property value.

This is also useful if you want to present a view on top of the exact size of a UIImage displayed in a UIImageView, with aspect fit mode.

How to use

Do not forget to include the AVFoundation framework.
Try this in a Playground:



  1. Apple Documentation
Happy Goals for iOS

Happy Goals for iOS

Want an app to help you track and achieve your goals, without the complex stuff, saving you from headaches? Checkout Happy Goals - Habits & Goals Tracker now in the App Store! Happy Goals has been featured in the App Store in over 32 countries. Start now, better late than never!


Thank you!

%d bloggers like this: