How to keep an Aspect-fixed UIView filling up the screen in any screen orientation programmatically in swift?
<p>I want just this simple effect:</p>

<p>that I have a picture automatically orients itself according to the device/screen orientation, it alway fill up the whole screen without stretching itself so that the aspect doesn't change.</p>

<p>I work with Xcode though I got a lot of answer with storyboard and UI builder,I really don't like such ways that work well but you have no idea how it works, so I was trying to find the way to do it programmatically instead.</p>

<p>I've tried to set a image view as subview, but it doesn't fix well after the device is rotated, it either stretch to fill the screen or move to somewhere else with size unchanged (when I trie to recalculate the frame and apply to the subview), then I tried to override the drawrect func, of the UIView, the frame works fine but the animation of rotating is broken (it stretches it self during the rotation and then skip into the final frame).</p>

<p>the perfect effect I want is just like iOS original photo app, when I check a photo, it resizes to fix the screen, and even when I rotate the screen, the animation works just perfect, the rotating and scaling happens in the same time and no skip of stretch happens. could anyone please give me some help?</p>

<p>THANKS!!!!!</p>

<p>the way I set the frame:</p>

<pre><code>func locating (imgsize:CGSize,scrsize:CGSize) -&gt;CGRect {
var size:CGSize
var ori:CGPoint
if ((imgsize.height / imgsize.width) * scrsize.width&gt;=scrsize.height){
    size = CGSize(width:scrsize.width,height:imgsize.height*(scrsize.width/imgsize.width))
    ori = CGPoint(x:0,y:scrsize.height-imgsize.height*(scrsize.width/imgsize.width))

}
else {
    size = CGSize(width:imgsize.width*(scrsize.height/imgsize.height),height:scrsize.height)
    ori = CGPoint(x:-(imgsize.width*(scrsize.height/imgsize.height)-scrsize.width)/2,y:0)
    }
return CGRect(origin:ori,size:size)
</code></pre>

<p>}</p>