0

I would like to add a share button to share a video (by mail for example) with the UIActivityController.

I tried to access to the properties of the AVPlayerViewController, but it seems we are not allowed to it anymore...

for subview in playerViewController.view.subviews 
    {
    }

How to add a simple button next to the play button on the right in the playbackControls?

Any ideas?

ΩlostA
  • 2,501
  • 5
  • 27
  • 63

1 Answers1

0

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.

nodebase
  • 2,510
  • 6
  • 31
  • 46