Rotate totally Landscape view when movie play in VLCPlayer with swift 3

前端 未结 2 1752
囚心锁ツ
囚心锁ツ 2021-01-26 03:46

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. Ma

相关标签:
2条回答
  • 2021-01-26 04:24

    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)
    }
    
    0 讨论(0)
  • 2021-01-26 04:29

    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.

    0 讨论(0)
提交回复
热议问题