简体   繁体   中英

iOS/Swift: How to add share button in the playbackControls of a AVPlayerViewController?

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?

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 force the view to load in many ways. The choice is yours. Another way of doing this is adding this just before adding your share button:

_ = playerViewController.view

instead of using the completion block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM