Swift UIKit,为什么视频播放器层不居中

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

不确定为什么这不是居中播放。我将 UIView“holderView”设为红色,因此您可以看到视频是播放器层或视频层对象未居中,偏向右侧。

这不会发生当

playerLayer.videoGravity = .resize
而不是 .resizeAspect

    func addPlayerLayer(){
        guard let videoPath = Bundle.main.path(forResource: "scan_animation", ofType: "mp4")
        else {
            print ("ERROR path for video not found")
            return
        }
        let videoView = UIView(frame: CGRect(x:0, y:0, width: holderView.frame.width, height: holderView.frame.height))
        let videoURL = NSURL(fileURLWithPath: videoPath)
        player = AVPlayer(url: videoURL as URL)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = videoView.frame
        playerLayer.videoGravity = .resizeAspect
        videoView.layer.addSublayer(playerLayer)
        holderView.addSubview(videoView)
        print("player layer frame \(playerLayer.frame)")
        print("player bounds \(playerLayer.bounds)")
        print("videoView frame \(videoView.frame)")
        print("videoView bounds \(videoView.bounds)")
        print("holderView frame \(holderView.frame)")
        print("holderView bounds \(holderView.bounds)")
    }

这是打印输出,一切似乎都居中,holderView 在前导和尾随处有 20 像素):

player layer frame (0.0, 0.0, 374.0, 449.0) 玩家边界 (0.0, 0.0, 374.0, 449.0) videoView 帧 (0.0, 0.0, 374.0, 449.0) videoView 边界 (0.0, 0.0, 374.0, 449.0) holderView 框架 (20.0, 48.0, 374.0, 449.0) holderView 边界 (0.0, 0.0, 374.0, 449.0)

我尝试了各种约束。

swift uikit xcode-storyboard
© www.soinside.com 2019 - 2024. All rights reserved.