首页 文章

当iOS在Swift中仅支持纵向方向时,如何在我的SDK VideoViewController中强制旋转到横向方向

提问于
浏览
0

我正在为视频播放器开发SDK . 但问题是视频自动旋转 . 当应用程序支持肖像和土地景观时,它工作正常 . 当app仅支持portrait app时,自动旋转失败 . 如何从SDK端执行强大的 .all 方向 .

一个解决方案是通过实现func应用程序(应用程序:UIApplication,supportedInterfaceOrientationsForWindow窗口:UIWindow?) - > AppDelegate中的UIInterfaceOrientationMask它将起作用 .

另一个解决方案是覆盖我的VideoViewController中的shouldAutorotate(),supportedInterfaceOrientations(),这仅适用于app同时支持(PORTRAIT和LANDSCAPE)方向的情况 .

但在我的情况下,SDK需要处理方向,因为我在任何可见控制器上方呈现VideoViewController . 并且我没有将我的VideoViewController暴露给应用程序 .

我怎样才能实现它..任何解决方案 .

1 回答

  • 0

    如果您的项目已经处于纵向状态,则无需进行任何更改 . 如果没有,请确保仅选择了纵向 .
    enter image description here

    在你的AppDelegate中添加:

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
            if let vcp = rootViewController as? ViewControllerRotateProtocol, vcp.canRotate() {
                // Unlock landscape view orientations for this view controller
    
                return [.landscapeLeft , .landscapeRight]
            }
        }
        return application.supportedInterfaceOrientations(for: window)
    }
    
    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
    }
    

    创建一个协议:您可以将此协议公开给应用程序 .

    protocol ViewControllerRotateProtocol {
        func canRotate() -> Bool
    }
    

    在您的View Controller中,添加以下代码:

    class ViewController: UIViewController, ViewControllerRotateProtocol {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func viewWillDisappear(_ animated : Bool) {
        super.viewWillDisappear(animated)
    
        if (self.isMovingFromParentViewController) {
            UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
        }
    }
    
    override var shouldAutorotate: Bool {
        return true
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return [.landscapeLeft , .landscapeRight]
    }
    
    func canRotate() -> Bool {
        return true
    } }
    

相关问题