0

I would like to rotate my view controller when movie play.
My question might be duplicate but I tried many ways when I find in stack overflow. But all codes do not work. May be I put the codes in wrong way.

 override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        //return[.portrait,.landscapeRight]
    return .landscapeLeft
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    //return UIInterfaceOrientation.landscapeRight
    return .landscapeLeft
}

I put this code to vlcplayerviewcontroller.swift file.

enter image description here

When user clicks on play button, then it will goes to vlcplayerviewcontroller.swift and I would like to show the movie in Landscape mode automatically. I have no idea of how can I do this.
My reference link is How to lock orientation of one view controller to portrait mode only in Swift.

Community
  • 1
  • 1
May Phyu
  • 895
  • 3
  • 23
  • 47

2 Answers2

2

Allow only Portrait Device Orientation under project setting. Add these lines into your AppDelegate file

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
        if (rootViewController.isKind(of: your_class_name_for_rotate.self)) {
            // Unlock landscape view orientations for this view controller
            return .allButUpsideDown;
        }
    }

    // Only allow portrait (standard behaviour)
    return .portrait;
}

private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
    if (rootViewController == nil) { return nil }
    if (rootViewController.isKind(of: UITabBarController.self)) {
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
    } else if (rootViewController.isKind(of: UINavigationController.self)) {
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
    } else if (rootViewController.presentedViewController != nil) {
        return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
    }
    return rootViewController
}

Add this line in viewDidLoad method of your view_controller_for_rotate

let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")

And add these methods in same class

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
    }
private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.landscapeLeft
    }
    private func shouldAutorotate() -> Bool {
        return true
    }

I hope this helps.

1

I had a solution in the linked answer, but I adjusted for landscape. Follow these steps and it will give you the desired functionality.

Swift 3

In AppDelegate:

/// set orientations you want to be allowed in this property by default
var orientationLock = UIInterfaceOrientationMask.all

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return self.orientationLock
}

In some other global struct or helper class, here I created AppUtility:

struct AppUtility {

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {

        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.orientationLock = orientation
        }
    }

    /// Added method to adjust lock and rotate to the desired orientation
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {

        self.lockOrientation(orientation)

        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }

}

Then in the desired ViewController you want to lock and rotate orientations, like your movie controller:

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // rotate and lock
    AppUtility.lockOrientation(.landscape, andRotateTo: .landscapeRight)

}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Don't forget to reset when view is being removed
    AppUtility.lockOrientation(.all)
}
bmjohns
  • 6,344
  • 1
  • 33
  • 39
  • Bro @bmjohns, Where do I put AppUtility code in app? Overlayviewcontroller.swift? – May Phyu Apr 10 '17 at 03:49
  • You can add it anywhere. But I would put it in a new swift file, so you can access the code anywhere in the app in case you want to rotate and lock any other time. – bmjohns Apr 10 '17 at 03:53
  • Bro @bmjohns, Movie player does not rotate :( – May Phyu Apr 10 '17 at 04:01
  • @MayPhyu Ok, well can you provide more info? What happens when you are debugging? Do you see the methods getting called when you expect them to? Do you have all rotations supported in your plist? – bmjohns Apr 10 '17 at 14:56
  • Bro @bmjohns, rotation problem was solved. But I encounter another problem http://stackoverflow.com/questions/43929485/play-forward-video-in-2x-3x-4x-speed-in-mobilevlckit-didnt-work . Could you please look at this? – May Phyu May 12 '17 at 06:56