Search code examples
iosswiftautolayoutuiinterfaceorientation

presentedViewController changes orientation of presenting view controller, breaks layout


I have a ViewController A that presents an MPMoviePlayerViewController. I want to force the video player to landscape, so I use this:

In the AppDelegate, supportedInterfaceOrientationsForWindow

if let viewController = self.window!.rootViewController?.presentedViewController {
            if let playbackController = viewController as? MPMoviePlayerViewController {
                return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue) | Int(UIInterfaceOrientationMask.LandscapeRight.rawValue)
            }
            return Int(UIInterfaceOrientationMask.Portrait.rawValue)
        }

In my video player subclass, I set the supported orientation:

override func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue) | Int(UIInterfaceOrientationMask.LandscapeRight.rawValue)
    }

When the video player is closed in landscape, however, View Controller A is stuck in landscape. How do I prevent View Controller A from switching to landscape, and keep it in portrait?


Solution

  • Just add

    override func supportedInterfaceOrientations() -> Int {
            return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }
    

    to every view controller that needs it and avoid AppDelegate based orientation support.