我正在开发一个iPhone(Swift)应用程序,我有一个WebView加载Youtube视频,我想只在视频全屏时允许横向模式。我的整个应用程序是视频未全屏播放时禁用纵向和旋转。我不希望用户能够在youtube视频未全屏播放时将应用程序旋转到横向,只有当视频以全屏模式播放时才会播放。
我尝试了以下代码,但它不起作用。
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
答案 0 :(得分:0)
supportedInterfaceOrientations
也不是一个可靠的解决方案。我所做的是在app delegate application supportedInterfaceOrientationsForWindow
方法中获取当前视图控制器并检查该控制器是否是支持所有方向的视图控制器,因此其返回值是根据此条件计算的
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
}
}
答案 1 :(得分:0)
我最近使用Zell B.的答案遇到了这个问题,这是一个Swift 5版本,它将检测AVPlayer并更新方向设置。
只需将以下代码复制并粘贴到您的AppDelegate中(无需编辑):
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let currentVC = getCurrentViewController(self.window?.rootViewController), currentVC is AVPlayerViewController {
return .allButUpsideDown
}
return .portrait
}
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)
}
return viewController
}