Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: AVPlayerViewController enable fullscreen rotation in portrait oriented application

I have a UIViewcontroller, which contains a AVPlayerViewController with AVPlayer. I want to enable rotation for AVPlayerViewController(when video is on fullscreen) and disable any rotation for UIViewController. How can I enable rotation only for videos(on fullscreen) in my app?

like image 777
ton252 Avatar asked Feb 27 '16 00:02

ton252


2 Answers

Swift 2.2 In AppDelegate to allow rotation for player:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        guard let vc = (window?.rootViewController?.presentedViewController) else {
            return .Portrait
        }

        if (vc.isKindOfClass(NSClassFromString("AVFullScreenViewController")!)) {
            return .AllButUpsideDown
        }

        return .Portrait
    }

Than create and use subclass of AVPlayerViewController for back to portrait mode when player exit full screen mode:

class YourVideoPlayer: AVPlayerViewController {

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if view.bounds == contentOverlayView?.bounds {
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
        }
    }
like image 105
Сергей Билык Avatar answered Oct 01 '22 18:10

Сергей Билык


For me this was better. Just extend AVPlayerViewController :

import AVKit
import UIKit

class AVPlayerViewControllerRotatable: AVPlayerViewController {

    override var shouldAutorotate: Bool {
        return true
    }

}

Swift 4

like image 26
ergunkocak Avatar answered Oct 01 '22 18:10

ergunkocak