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?
            Asked
            
        
        
            Active
            
        
            Viewed 9,646 times
        
    14
            
            
        - 
                    Have you implemented `supportedInterfaceOrientations` appropriately? – matt Feb 27 '16 at 00:04
- 
                    @matt I just return UIInterfaceOrientationMaskPortrait in supportedInterfaceOrientations method. UIViewController does not rotate and AVPlayer to. – ton252 Feb 27 '16 at 00:44
3 Answers
15
            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")
        }
    }
 
    
    
        Serge Bilyk
        
- 875
- 9
- 10
- 
                    1
- 
                    >= iOS 10, AVPlayerViewController handles landscape support for video playback in portrait-only apps, thus you should avoid using this hack. – dferrero May 03 '18 at 23:06
3
            
            
        For me this was better. Just extend AVPlayerViewController :
import AVKit
import UIKit
class AVPlayerViewControllerRotatable: AVPlayerViewController {
    override var shouldAutorotate: Bool {
        return true
    }
}
Swift 4
 
    
    
        ergunkocak
        
- 3,334
- 1
- 32
- 31
- 
                    1way much easier than dealing with rotation orientation in app delegate level! – joe Sep 28 '19 at 14:02
0
            
            
        You have to enable the allowable rotations at the project level and then restrict rotation on all the viewControllers you DON'T want to rotate. i.e. if you have 5 viewControllers, you'll need to restrict rotation on 4 of them and only allow rotation on the Player Controller. You can see more on this here Handling autorotation for one view controller in iOS7
 
     
    