Swift - How to enable landscape in full screen video?
Asked Answered
T

2

0

I am working on an iPhone(Swift) app and I have a WebView which loads a Youtube video and I would like to allow landscape mode only when the video is full screen. My whole app is portrait and rotation is disabled when the video is not playing in full screen. I don't want the user to be able to rotate the app to landscape when the youtube video is not in full screen, only when the video is playing in full screen.

I tried the following code, but it doesn't work.

override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
Truly answered 5/6, 2015 at 10:6 Comment(0)
P
1

supportedInterfaceOrientations was not such a reliable solution in my case as well. What I did was getting current view controller in app delegate application supportedInterfaceOrientationsForWindow method and checked if that controller was a view controller that supported all orientations so its return value was calculated based on this condition

 func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {

    if let currentVC = getCurrentViewController(self.window?.rootViewController){

        //VideoVC is the name of your class that should support landscape
        if currentVC is VideoVC{

            return Int(UIInterfaceOrientationMask.All.rawValue)
        }
    }
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

func getCurrentViewController(viewController:UIViewController?)-> UIViewController?{

     if let tabBarController = viewController as? UITabBarController{

        return getCurrentViewController(tabBarController.selectedViewController)
    }

    if let navigationController = viewController as? UINavigationController{
        return getCurrentViewController(navigationController.visibleViewController)
    }

    if let viewController = viewController?.presentedViewController {

        return getCurrentViewController(viewController)

    }else{

        return viewController
    }
}
Prentice answered 5/6, 2015 at 10:55 Comment(14)
should I put this in my AppDelegate or in my viewController?Truly
thanks. there's only one error now, please check: postimg.org/image/5rntzarzzTruly
Fixed now, check it againPrentice
thanks again. No errors this time, but the app crashes when I load the app: postimg.org/image/yqwd7jvw7Truly
no errors and crashes this time, but when i open the youtube video, i can't rotate it to landscapeTruly
Have you removed supportedInterfaceOrientations from test VC ?Prentice
yes. I did and only portrait mode is selected in the project settings.Truly
Thats the problem, select all modes in project settingsPrentice
yes I did. Now everything is in portrait and cannot be rotated but the video can't be rotated either unfortunatelyTruly
here's my project, just click on test on the tab bar to see the video: filedropper.com/motivatemeTruly
@MartonZeisler found the problem, didn't know that you were using tabbarcontroller, check the edited answer once morePrentice
yes it's better now. But I only want the user to be able to rotate to landscape if the video is playing. Right now I can rotate the screen even when I didn't even start watching the video. The screen should only be allowed to rotated when the webview's video is playing full screen. Once i close the video I shouldn't be able to rotate the screen to landscape. postimg.org/image/q4hxrcd4fTruly
Any luck with making rotation video-only?Fulminant
any luck guys? I have the same issue ... :(Dotson
M
0

I came across this problem recently and using Zell B.'s answer, here is a Swift 5 version that will detect an AVPlayer and update the orientation settings.

Just copy and paste the below code into your AppDelegate (no editing necessary):

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if let currentVC = getCurrentViewController(self.window?.rootViewController), currentVC is AVPlayerViewController {
        return .allButUpsideDown
    }
    return .portrait
}

func getCurrentViewController(_ viewController: UIViewController?) -> UIViewController? {

    if let tabBarController = viewController as? UITabBarController {
        return getCurrentViewController(tabBarController.selectedViewController)
    }

    if let navigationController = viewController as? UINavigationController{
        return getCurrentViewController(navigationController.visibleViewController)
    }

    if let viewController = viewController?.presentedViewController {
        return getCurrentViewController(viewController)
    }
    
    return viewController
}
Middlebreaker answered 17/7, 2020 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.