iOS:AVPlayerViewController在纵向应用程序中启用全屏旋转

14

我有一个包含AVPlayerViewController和AVPlayer的UIViewcontroller。 我想在AVPlayerViewController(视频全屏时)中启用旋转并禁用UIViewController的任何旋转。 如何仅在应用程序中的视频(全屏时)中启用旋转?


你是否已经适当地实现了 supportedInterfaceOrientations - matt
@matt 我只是在supportedInterfaceOrientations方法中返回UIInterfaceOrientationMaskPortrait。UIViewController不会旋转,AVPlayer也不会。 - ton252
3个回答

15

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
    }

创建并使用AVPlayerViewController的子类,在播放器退出全屏模式时返回至竖屏模式:

class YourVideoPlayer: AVPlayerViewController {

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if view.bounds == contentOverlayView?.bounds {
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
        }
    }

1
太棒了!你在哪里找到关于AVPlayer的信息? - thibaut noah
在iOS 10及以上版本中,AVPlayerViewController可以在仅支持竖屏的应用中处理横屏的视频播放支持,因此您应避免使用此类技巧。 - dferrero

3

对我来说,这样会更好。只需扩展AVPlayerViewController

import AVKit
import UIKit

class AVPlayerViewControllerRotatable: AVPlayerViewController {

    override var shouldAutorotate: Bool {
        return true
    }

}

Swift 4


1
比在应用程序委托层处理旋转方向要容易得多! - joe

0
你必须在项目级别上启用允许旋转,并限制所有你不想旋转的视图控制器。例如,如果你有5个视图控制器,你需要在其中4个上限制旋转,只允许在“Player Controller”上旋转。你可以在这里查看更多 iOS7中处理一个视图控制器的自动旋转

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接