繁体   English   中英

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

[英]How to rotate forcefully in my SDK VideoViewController to landscape orientation when app supports only portrait orientation in iOS Swift

正在开发视频播放器的SDK。 但是问题是视频自动旋转。 当应用同时支持纵向和横向景观时,它可以正常工作。 如果应用仅支持纵向应用,则自动旋转失败。 我该如何从SDK方面进行强制的.all定向。

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

另一种解决方案是在我的VideoViewController中重写shouldAutorotate(),supportedInterfaceOrientations(),仅当应用程序同时支持PORTRAIT和LANDSCAPE方向时,此方法才有效。

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

我怎样才能实现呢。任何解决方案。

如果您的项目已经处于纵向状态,则无需进行任何更改。 如果没有,请确保仅选择肖像。 在此处输入图片说明

在您的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
} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM