我正在构建一个屏幕,用户可以使用AVPlayerViewController播放音频文件。问题是我无法在播放器视图中删除QuickTime徽标,请参见屏幕截图:
我的代码:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
guard let url = URL(string: filePath!) else {
return
}
let player = AVPlayer(url: url)
let controller = AVPlayerViewController()
controller.modalPresentationStyle = .overFullScreen
controller.player = player
self.present(controller, animated: true) {
player.play()
}
} catch {
}
我尝试使用controller.contentOverlayView?.addSubView()
添加UIImageView,但我无法正确地将其集中。如何自定义播放器的布局而无需从头开始构建我自己的界面?
答案 0 :(得分:3)
您尝试的是正确的:只需将子视图添加到内容叠加视图即可。但是您省略了一步:您必须同时提供子视图和内容叠加视图约束,以使它们完全覆盖播放器控制器的视图。
示例(我的av
是您的controller
):
let iv = UIView()
iv.backgroundColor = .white
av.contentOverlayView!.addSubview(iv)
let v = av.contentOverlayView!
iv.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
iv.bottomAnchor.constraint(equalTo:v.bottomAnchor),
iv.topAnchor.constraint(equalTo:v.topAnchor),
iv.leadingAnchor.constraint(equalTo:v.leadingAnchor),
iv.trailingAnchor.constraint(equalTo:v.trailingAnchor),
])
NSLayoutConstraint.activate([
v.bottomAnchor.constraint(equalTo:av.view.bottomAnchor),
v.topAnchor.constraint(equalTo:av.view.topAnchor),
v.leadingAnchor.constraint(equalTo:av.view.leadingAnchor),
v.trailingAnchor.constraint(equalTo:av.view.trailingAnchor),
])