How to make a AVPlayerViewController go to fullscreen programmatically?

前端 未结 9 1094
既然无缘
既然无缘 2020-12-29 20:12

I\'m trying to make a AVPlayerViewController go to full screen mode programmatically, coming from \"embedded\" mode, however this does not appear to be possible with the pub

相关标签:
9条回答
  • 2020-12-29 21:03

    Swift 3 version for the answer of ToddH:

    extension AVPlayerViewController {
    
        func goFullScreen() {
            let selector = NSSelectorFromString("_transitionToFullScreenViewControllerAnimated:completionHandler:")
            if self.responds(to: selector) {
                // first argument is animated (true for me), second is completion handler (nil in my case)
                self.perform(selector, with: true, with: nil)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 21:09

    UPDATE: Swift 4 version of ToddH's answer:

    private func enterFullscreen(playerViewController: AVPlayerViewController) {
    
        let selectorName: String = {
            if #available(iOS 11.3, *) {
                return "_transitionToFullScreenAnimated:interactive:completionHandler:"
            } else if #available(iOS 11, *) {
                return "_transitionToFullScreenAnimated:completionHandler:"
            } else {
                return "_transitionToFullScreenViewControllerAnimated:completionHandler:"
            }
        }()
        let selectorToForceFullScreenMode = NSSelectorFromString(selectorName)
    
        if playerViewController.responds(to: selectorToForceFullScreenMode) {
            playerViewController.perform(selectorToForceFullScreenMode, with: true, with: nil)
        }
    }
    
    0 讨论(0)
  • 2020-12-29 21:09

    I did not have the need to use any restricted code.

    For this, I am assuming that you have added the AVPlayerViewController as a child view controller.

    Then for that you will first have to remove the child view controller and then present it again as a fullscreen controller as well attach the AVPlayer view properly to it's parent view.

    Here is how I did it. Please note that I am using a library called Easy Peasy for restoring the playerVC.view constraints - one can do that with proper constraints as well.

        @objc func fullscreenButtonClicked() {
    
            playerVC.willMove(toParentViewController: nil)
            playerVC.view.removeFromSuperview()
            playerVC.removeFromParentViewController()
    
    
            self.present(self.playerVC, animated: false, completion: {
                self.playerVC.view.easy.layout(Top(), Right(), Left(), Bottom())
            })
        }
    
    
    0 讨论(0)
提交回复
热议问题