iOS - 调整视图层大小以填充 iPhone X 最大背景
iOS - resize view layer to fill iPhone X Max background
我关注 是为了让视图填充背景。
所以我使用了一个名为 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()
}
}
结果:
播放器图层背景默认透明。如果你想隐藏后面的蓝色视图,请尝试在播放器层上设置背景颜色。
playerLayer.backgroundColor = UIColor.black.cgColor
最后我使用:
playerLayer.videoGravity = AVLayerVideoGravity.resize
我关注
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()
}
}
结果:
播放器图层背景默认透明。如果你想隐藏后面的蓝色视图,请尝试在播放器层上设置背景颜色。
playerLayer.backgroundColor = UIColor.black.cgColor
最后我使用:
playerLayer.videoGravity = AVLayerVideoGravity.resize