gpt4 book ai didi

ios - Swift - 如何在全屏视频中启用横向?

转载 作者:行者123 更新时间:2023-11-30 14:19:32 27 4
gpt4 key购买 nike

我正在开发一个 iPhone(Swift) 应用程序,我有一个加载 Youtube 视频的 WebView,我希望仅在视频全屏时才允许横向模式。 我的整个应用程序是当视频未全屏播放时,纵向和旋转会被禁用。我不希望用户在 YouTube 视频未全屏时才能将应用旋转为横向,而只有在视频全屏播放时才能将应用旋转为横向。

我尝试了以下代码,但它不起作用。

override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

最佳答案

supportedInterfaceOrientations 在我的情况下也不是一个可靠的解决方案。我所做的是在应用程序委托(delegate) applicationsupportedInterfaceOrientationsForWindow 方法中获取当前 View Controller ,并检查该 Controller 是否是支持所有方向的 View Controller ,以便根据此条件计算其返回值

 func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {

if let currentVC = getCurrentViewController(self.window?.rootViewController){

//VideoVC is the name of your class that should support landscape
if currentVC is VideoVC{

return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

func getCurrentViewController(viewController:UIViewController?)-> UIViewController?{

if let tabBarController = viewController as? UITabBarController{

return getCurrentViewController(tabBarController.selectedViewController)
}

if let navigationController = viewController as? UINavigationController{
return getCurrentViewController(navigationController.visibleViewController)
}

if let viewController = viewController?.presentedViewController {

return getCurrentViewController(viewController)

}else{

return viewController
}
}

关于ios - Swift - 如何在全屏视频中启用横向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30663828/

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