【问题标题】:How to rotate forcefully in my SDK VideoViewController to landscape orientation when app supports only portrait orientation in iOS Swift当应用程序在 iOS Swift 中仅支持纵向时,如何在我的 SDK VideoViewController 中强制旋转为横向
【发布时间】:2018-09-07 13:54:51
【问题描述】:

正在为视频播放器开发 SDK。但问题是视频自动旋转。当应用程序同时支持纵向和横向时,它可以正常工作。当应用仅支持纵向应用时,自动旋转失败。我如何从 SDK 端进行有力的 .all 方向。

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

另一种解决方案是在我的 VideoViewController 中覆盖 shouldAutorotate()、supportedInterfaceOrientations() 这仅在应用同时支持(纵向和横向)方向时才有效。

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

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

【问题讨论】:

  • 这不是那个问题的重复。应用程序仅支持纵向。 SDK 想要将 UIViewController 旋转到 Landscape。

标签: ios swift uiviewcontroller orientation screen-orientation


【解决方案1】:

如果您的项目已经是纵向的,则无需进行任何更改。如果没有,请确保只选择肖像。

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

在您的视图控制器中,添加以下代码:

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
} }

【讨论】:

  • 当 AppDelegate 知道控制器类时有效。但是这里 AppDelegate 不知道 ViewController 类名。 SDK 将 VideoViewController 呈现在任何呈现的控制器之上。
猜你喜欢
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
相关资源
最近更新 更多