Fayza Nawaz
Fayza Nawaz

Reputation: 2316

AVPlayer View Controller Autorotation off?

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?

Upvotes: 4

Views: 1529

Answers (2)

Pramuka Dias
Pramuka Dias

Reputation: 568

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

Upvotes: 0

Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

Create new class that inherits from AVPlayerViewController and implement the supported interface method like this. After this instead of initializing object of AVPlayerViewController just create object of the class that you have created.

class MyPortraitVideoController: AVPlayerViewController {


    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}


Edited to add:

Note that the Documentation for AVPlayerViewController states:

Important

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.

As this is the accepted answer, I assume the OP has tried it and it worked, but I thought I should add a note about it here to make sure future readers are aware of any potential problems. Undefined behavior means this could stop working in a future release of iOS.

Upvotes: 5

Related Questions