I have a UIViewcontroller, which contains a AVPlayerViewController with AVPlayer. I want to enable rotation for AVPlayerViewController(when video is on fullscreen) and disable any rotation for UIViewController. How can I enable rotation only for videos(on fullscreen) in my app?
我有一个UIViewcontroller,它包含一个带AVPlayer的AVPlayerViewController。我想为AVPlayerViewController启用旋转(当视频在全屏时)并禁用UIViewController的任何旋转。如何在我的应用中为视频(全屏)启用旋转?
3 个解决方案
#1
13
Swift 2.2 In AppDelegate to allow rotation for player:
Swift 2.2在AppDelegate中允许玩家轮换:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
guard let vc = (window?.rootViewController?.presentedViewController) else {
return .Portrait
}
if (vc.isKindOfClass(NSClassFromString("AVFullScreenViewController")!)) {
return .AllButUpsideDown
}
return .Portrait
}
Than create and use subclass of AVPlayerViewController for back to portrait mode when player exit full screen mode:
当播放器退出全屏模式时,创建并使用AVPlayerViewController的子类返回纵向模式:
class YourVideoPlayer: AVPlayerViewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if view.bounds == contentOverlayView?.bounds {
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
}
}
#2
0
You have to enable the allowable rotations at the project level and then restrict rotation on all the viewControllers you DON'T want to rotate. i.e. if you have 5 viewControllers, you'll need to restrict rotation on 4 of them and only allow rotation on the Player Controller. You can see more on this here Handling autorotation for one view controller in iOS7
您必须在项目级别启用允许的旋转,然后限制您不想旋转的所有viewControllers的旋转。即如果你有5个viewControllers,你需要限制其中4个的旋转,并且只允许在Player Controller上旋转。您可以在此处查看更多内容在iOS7中处理一个视图控制器的自动旋转
#3
0
For me this was better. Just extend AVPlayerViewController :
对我来说这更好。只需扩展AVPlayerViewController:
import AVKit
import UIKit
class AVPlayerViewControllerRotatable: AVPlayerViewController {
override var shouldAutorotate: Bool {
return true
}
}
Swift 4
#1
13
Swift 2.2 In AppDelegate to allow rotation for player:
Swift 2.2在AppDelegate中允许玩家轮换:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
guard let vc = (window?.rootViewController?.presentedViewController) else {
return .Portrait
}
if (vc.isKindOfClass(NSClassFromString("AVFullScreenViewController")!)) {
return .AllButUpsideDown
}
return .Portrait
}
Than create and use subclass of AVPlayerViewController for back to portrait mode when player exit full screen mode:
当播放器退出全屏模式时,创建并使用AVPlayerViewController的子类返回纵向模式:
class YourVideoPlayer: AVPlayerViewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if view.bounds == contentOverlayView?.bounds {
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
}
}
#2
0
You have to enable the allowable rotations at the project level and then restrict rotation on all the viewControllers you DON'T want to rotate. i.e. if you have 5 viewControllers, you'll need to restrict rotation on 4 of them and only allow rotation on the Player Controller. You can see more on this here Handling autorotation for one view controller in iOS7
您必须在项目级别启用允许的旋转,然后限制您不想旋转的所有viewControllers的旋转。即如果你有5个viewControllers,你需要限制其中4个的旋转,并且只允许在Player Controller上旋转。您可以在此处查看更多内容在iOS7中处理一个视图控制器的自动旋转
#3
0
For me this was better. Just extend AVPlayerViewController :
对我来说这更好。只需扩展AVPlayerViewController:
import AVKit
import UIKit
class AVPlayerViewControllerRotatable: AVPlayerViewController {
override var shouldAutorotate: Bool {
return true
}
}
Swift 4