4

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?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Fayza Nawaz
  • 2,256
  • 3
  • 26
  • 61

2 Answers2

5

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.

Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52
  • yes, i just tried this and was about to post the answer. Thanks :) – Fayza Nawaz May 17 '17 at 15:10
  • 1
    Then i must've up vote u too. 'Cause you've got your answer without anyones help. – Syed Qamar Abbas May 17 '17 at 15:11
  • 1
    Seems like the right answer, except for this: https://developer.apple.com/reference/avkit/avplayerviewcontroller. "Important: Do not subclass AVPlayerViewController. Overriding this class's methods is unsupported and results in undefined behavior." – Jeff Loughlin May 17 '17 at 16:57
  • @JeffLoughlin, thanks for the comment. In this scenerio, do you have any idea how can i fix the issue (Without using subclass) ? – Fayza Nawaz Jun 01 '17 at 11:25
0

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
Pramuka Dias
  • 568
  • 4
  • 16