iOS - 调整视图层大小以填充iPhone X Max背景

问题描述 投票:0回答:2

我跟随https://stackoverflow.com/a/46337372/2139691,以便填补背景。所以我使用一个名为myview的视图,蓝色背景,但是当我添加一个视频图层时,我的结果很糟糕。

class MovViewController: UIViewController {

    var avPlayer: AVPlayer!
    @IBOutlet weak var myview: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let filepath: String? = Bundle.main.path(forResource: "video_fondo", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        let player = AVPlayer(url: fileURL)
        let playerLayer = AVPlayerLayer(player: player)

        playerLayer.frame = myview.bounds
        myview.layer.insertSublayer(playerLayer, at: 0)
        player.play()

    }
}

结果:enter image description here

ios avplayer avplayerlayer safearealayoutguide
2个回答
1
投票

默认情况下,播放器图层背景是透明的。如果要隐藏后面的蓝色视图,请尝试在播放器图层上设置背景颜色。

playerLayer.backgroundColor = UIColor.black.cgColor

1
投票

最后我得到它使用:

playerLayer.videoGravity = AVLayerVideoGravity.resize
© www.soinside.com 2019 - 2024. All rights reserved.