Search code examples
iosswiftavplayeravplayerlayer

Set AVPlayer/AVPlayerLayer size using AutoLayout?


I'm trying to set the size/frame of the video I'm showing in my view, using AutoLayout. But I can't figure out how to do it properly. This is the code for my view:

import UIKit
import AVKit

class VideoMeetingView: UIView {

    lazy var playerLayer: AVPlayerLayer = {
        let layer = AVPlayerLayer()
        return layer
    }()

    private lazy var videoView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .blue
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
        self.addSubviewsAndConstraints()

        playerLayer.frame = videoView.bounds
        videoView.layer.addSublayer(playerLayer)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func addSubviewsAndConstraints() {
        self.addSubview(videoView)

        videoView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        videoView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        videoView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        videoView.heightAnchor.constraint(equalToConstant: 600).isActive = true
    }

}

If I print out the values of videoView.bounds it's just (0, 0, 0, 0), so I guess AutoLayout does not update value as I first thought.

If I set the size of playerLayer.frame manually using CGRect everything works as it should. But I want to use AutoLayout.

So how can I set AvPlayerLayer's size using AutoLayout?


Solution

  • Do:

    self.playerLayer = {
       let layer = AVPlayerLayer(player: self.player)
        layer.videoGravity = .resizeAspect
        layer.needsDisplayOnBoundsChange = true
        return layer
    }()
    

    which causes the layer to resize aspect fit when the bounds of its parent changes.

    Also, you MIGHT have to add:

    override func layoutSubviews() {
        super.layoutSubviews()
    
        self.playerLayer.frame = self.bounds
    }
    

    so that it has the right frame no matter what..

    Another option is to make the AVPlayerLayer the actual layer of the UIView itself instead of a sub-layer:

    class VideoView : UIView
    {
        override var layer: CALayer {
            return AVPlayerLayer.class
        }
    }