Tiny Dragon
Tiny Dragon

Reputation: 71

Exit AVPlayerViewController fullscreen programmatically?

I am try customize AVPlayerViewController show fullscreen and I search link: How to make a AVPlayerViewController go to fullscreen programmatically?. It work when I use private method, but I can't find method exit full screen.

Upvotes: 2

Views: 3233

Answers (2)

Martin
Martin

Reputation: 1133

I have found a solution to this problem that works for me

extension AVPlayerViewController {

func goFullScreen() {
    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 self.responds(to: selectorToForceFullScreenMode) {
        self.perform(selectorToForceFullScreenMode, with: true, with: nil)
    }
}

func quitFullScreen() {
    let selectorName: String = {
        if #available(iOS 11, *) {
            return "_transitionFromFullScreenAnimated:completionHandler:"
        } else {
            return "_transitionFromFullScreenViewControllerAnimated:completionHandler:"
        }
    }()
    let selectorToForceQuitFullScreenMode = NSSelectorFromString(selectorName)

    if self.responds(to: selectorToForceQuitFullScreenMode) {
        self.perform(selectorToForceQuitFullScreenMode, with: true, with: nil)
    }
}
}

Hope it helps.

Upvotes: 2

Rafaela Lourenço
Rafaela Lourenço

Reputation: 1166

I found a solution that you can go and exit fullscreen.

If someone look for an answer, I hope it helps...

import AVKit

extension AVPlayerViewController {

    func goFullScreen() {
        let selectorName = "enterFullScreenAnimated:completionHandler:"
        let selectorToForceFullScreenMode = NSSelectorFromString(selectorName)

        if self.responds(to: selectorToForceFullScreenMode) {
            self.perform(selectorToForceFullScreenMode, with: true, with: nil)
        }
    }

    func quitFullScreen() {
        let selectorName  "exitFullScreenAnimated:completionHandler:"
        let selectorToForceQuitFullScreenMode = NSSelectorFromString(selectorName)

        if self.responds(to: selectorToForceQuitFullScreenMode) {
            self.perform(selectorToForceQuitFullScreenMode, with: true, with: nil)
        }
    } 
}

Let me know if work for you.

Upvotes: 1

Related Questions