Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get supportedInterfaceOrientationsForWindow to work with Swift 1.2

Hey Stackoverflow Members, maybe you could help me to get my problem fixed.

The problem is I want to lock the orientation for all UIViewControllers to "Portrait" but if the MoviePlayer appears it should switch into landscape mode and back if the movie player disappears.

Until Swift 1.2 I used:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> UIInterfaceOrientationMask {
//If the video is being presented, let the user change orientation, otherwise don't.
if let presentedViewController = window.rootViewController?.presentedViewController? {
    if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
        return .AllButUpsideDown
    }
}
return .Portrait
}

With Swift 1.2 some things changed and so I ended up with the following code:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    //If the video is being presented, let the user change orientation, otherwise don't.
    if let presentedViewController = window?.rootViewController?.presentedViewController {
        if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
            return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
        }
    }
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

But my code doesn't work, the Movie Player (XCDYoutube) is locked in portrait mode. Device orientation Settings should be fine!

Thanks in advance for your help!

like image 357
mort3m Avatar asked Apr 16 '15 13:04

mort3m


2 Answers

I had similar logic to yours but ending up returning support for all orientations.

return UIInterfaceOrientationMaskAll in appdelegate.

Depending on how many view controllers you have - you may want to create an abstract subclass of UIViewController and return only support for Portrait / and then hack your youtube view controller to support landscape.

  • (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; }
like image 185
johndpope Avatar answered Nov 15 '22 03:11

johndpope


I just had the exact same issue. I found a way to fix it by reaching the top of the controller stack:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    //If the video is being presented, let the user change orientation, otherwise don't.
    if var presentedViewController = window?.rootViewController?.presentedViewController {
        // Get the controller on the top of the stack
        while (presentedViewController.presentedViewController) != nil {
            presentedViewController = presentedViewController.presentedViewController!
        }

        if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
            return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
        }
    }
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

You can also try to display the type of presentedViewController to be sure it's the right one:

println("presentedViewController type: \(presentedViewController.dynamicType)")
like image 41
Melvin Avatar answered Nov 15 '22 03:11

Melvin