If you look at the view controller's view's subviews, you will see its a single view called AVPlayerViewControllerContentView
. You can do something like this when making your view controller:
func showFullScreenVideo(using player: AVPlayer) {
// setup your player view controller
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) { [weak self] in
self?.addShareButton(to: playerViewController)
}
}
private func addShareButton(to playerViewController: AVPlayerViewController) {
if let playerContentView = playerViewController.view.subviews.first {
let someView = SomeView()
playerContentView.addSubview(someView)
// use your own layout code:
someView.constrain(.centerX, .centerY, to: playerContentView)
}
}
Adding the share button inside the completion block ensures that playerViewController.view
is loaded and available to use. You can try to force the view to load in many ways. The choice is yours. Another possible way of doing this is adding this line of code just before adding your share button:
_ = playerViewController.view
instead of using the completion block. May or may not work, but the completion block method definitely works.