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?
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.
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.
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.
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....
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;
}
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")
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With