gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:37:54 25 4
gpt4 key购买 nike

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

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

还有一个解决方案是在我的 VideoViewController 中覆盖 shouldAutorotate()、supportedInterfaceOrientations() ,这仅当应用程序也支持两种方向(纵向和横向)时才有效。

但就我而言,SDK 需要处理方向,因为我将 VideoViewController 呈现在任何可见 Controller 之上。并且我没有将我的 VideoViewController 暴露给应用程序。

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

最佳答案

如果您的项目已经是纵向的,则无需进行任何更改。如果没有,请确保仅选择纵向。 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):您可以将此协议(protocol)公开给应用程序。

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

关于ios - 当应用程序在 iOS Swift 中仅支持纵向方向时,如何在我的 SDK VideoViewController 中强制旋转到横向方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49541977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com