AVPlayer layer inside a view does not resize when UIView frame changes

  playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFit;

Converting @Suran's solution to Swift:

First, create a class inheriting UIView where you override only 1 variable:

import UIKit
import AVFoundation

class AVPlayerView: UIView {
    override class var layerClass: AnyClass {
        return AVPlayerLayer.self
    }
}

Then add a simple UIView using the interface builder and change its class to the one you just created: AVPlayerView

change class of regular UIView to AVPlayerView

Then, make an outlet for that view. Call it avPlayerView

@IBOutlet weak var avPlayerView: AVPlayerView!

Now, you can use that view inside your viewcontroller and access its avlayer like this:

let avPlayer = AVPlayer(url: video)
let castLayer = avPlayerView.layer as! AVPlayerLayer
castLayer.player = avPlayer
avPlayer.play()

The layer will now follow the constraints just like a regular layer would do. No need to manually change bounds or sizes.


Actually you shouldn't add AVPlayer's layer as a sublayer. Instead of that you should use the following method, in the subclass of view in which you want to display AVPlayer.

+ (Class)layerClass
{
     return [AVPlayerLayer class];
}

And use the following line to add(set) the player layer.

[(AVPlayerLayer *)self.layer setPlayer:self.avPlayer];

Hope it helps;-)