AVPlayer View Controller Autorotation off?

Viewed 1333

I'm working on an iOS app in portrait mode only, and it's working fine.

Issue is that I'm presenting AVPlayerViewController above navigationController (both created programmatically). This player supports all the rotations (I don't know why!).

I want to fix this to portrait mode only just like other sections of app.

How can we do that?

2 Answers

In my case, I had to restricted landscape orientation for some views.So I set supportedInterfaceOrientationsFor as portrait in AppDelegate. When I present AVPlayerViewController I had to set supportedInterfaceOrientationsFor as all in AppDelegate. Check below codes as well.

AppDelegate.swift

var shouldSupportLandscape = Bool()

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
     if self.shouldSupportLandscape == true {
          return .all
      }else {
          return .portrait
      }   
 }

When present AVPlayerViewController

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldSupportLandscape = true

When dismiss AVPlayerViewController

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldSupportLandscape = false
Related