'Swift AVPlayerLayer videoGravity .resizeAspect only respects height not width

I have an AVPlayer and AVPlayerLayer embedded in a simple UIView. This plays the movie but adjusting playerLayer.videoGravity only effects the movies relative height:

  1. .resizeAspect fills all the way to side of the videoview UIView and crops the height
  2. .resizeAspectFill looks like a zoomed in version of the stretched .resizeAspect
    var player = AVPlayer()
    @IBOutlet weak var videoview: UIView!
    override func viewDidAppear(_ animated: Bool) {

        player = AVPlayer(url: url)
        let playerLayer = AVPlayerLayer(player: player)
                
        playerLayer.setAffineTransform(CGAffineTransform(rotationAngle: CGFloat((-90 * Double.pi)/180)))
                                       
        playerLayer.frame = videoview.bounds
        playerLayer.videoGravity = .resizeAspect

        videoview.layer.addSublayer(playerLayer)     
        
        player.play()
    }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source