Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 - change orientation back to portrait when dismissing full screen AVPlayerViewController

my app is a portrait only app, but I have one view controller which is displaying a live stream, using an AVPlayerViewController.

To allow landscape for the full screen view of that player I wrote this method in the AppDelegate.swift:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    var orientation =  UIInterfaceOrientationMask.Portrait

    if let presentedController = window?.rootViewController?.presentedViewController? {

        if presentedController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) {
            orientation = .AllButUpsideDown
        } else if let navController = presentedController as? UINavigationController {
            if navController.topViewController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) {
               orientation = .AllButUpsideDown
            }
        }
    }

    return  Int(orientation.rawValue)
}

This is how I call initialise my AVPlayer:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showLiveStream" {

        SVProgressHUD.show()
        var queue: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

        dispatch_async(queue, {
            let streamURL = NSURL(string: liveStreamURL)
            let playerItem = AVPlayerItem(URL: streamURL)
            let player = AVPlayer(playerItem: playerItem)

            dispatch_async(dispatch_get_main_queue(), {
                SVProgressHUD.dismiss()
                var playerViewController = segue.destinationViewController as AVPlayerViewController
                playerViewController.player = player
            })
        })
    }
}

The problem: when I open up the full screen view of the player, then change to landscape and then click "Done" to dismiss the full screen view, my app stays in landscape. But I want it to rotate to portrait again. How can I do that?

like image 301
flo_23 Avatar asked Dec 22 '14 00:12

flo_23


People also ask

How do I Turn Off portrait orientation on my iPhone?

1 Swipe down from the top-right corner of your screen to open Control Center. 2 Tap the Portrait Orientation Lock button to make sure that it's off. 3 Turn your iPhone sideways.

What do I do if the screen won't rotate on iOS?

If the screen on your iOS device won't rotate, learn what to do. Make sure that Portrait Orientation Lock is turned off. To check, open Control Center. If you see , tap it to turn Portrait Orientation Lock off.

How do I change the orientation of my screen on iPhone?

Learn more. If you have an iPhone Plus, and want the Home screen to rotate, go to Settings > Display & Brightness and set Display Zoom to Standard. If you have an iPad with a Side Switch, you can set the Side Switch to work as a rotation lock or mute switch. Go to Settings > General.

How do I Fix my iPhone or iPod touch screen orientation problem?

1 Swipe up from the bottom edge of your screen to open Contol Center. 2 Tap the Portrait Orientation Lock button to make sure that it's off. 3 Turn your iPhone or iPod touch sideways. If the screen still won't rotate, try another app — like Safari or Messages — which are known to work in landscape mode. See More....


2 Answers

Rather than implementing application(_:supportedInterfaceOrientationsForWindow:) try implementing supportedInterfaceOrientations() on each view controller. So for example:

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

This will ensure that the view controller cannot be displayed in landscape, so when dismissing the video player, it will come straight back to a portrait window.

Update Objective-C:

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 
like image 130
Doug Avatar answered Oct 10 '22 05:10

Doug


What i did to fix this issue, create a new class with a 'viewDidLayoutSubviews' function and override it!

final class NewMoviePlayerViewController: AVPlayerViewController {
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        if view.bounds == contentOverlayView?.bounds {
            let value = UIInterfaceOrientation.portrait.rawValue
            UIDevice.current.setValue(value, forKey: "orientation")
        }
    }
}
like image 41
J. Zonneveld Avatar answered Oct 10 '22 03:10

J. Zonneveld