Rotate totally Landscape view when movie play in V

2020-05-03 11:07发布

问题:

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.

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.

回答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)
}


回答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.