AVPlayer 视图控制器自动旋转关闭?

Fay*_*waz 4 autorotate ios swift avplayerviewcontroller

我正在开发一个仅在纵向模式下的 iOS 应用程序,并且运行良好。

问题是我在 navigationController 之上呈现AVPlayerViewController(两者都是以编程方式创建的)。该播放器支持所有轮换(我不知道为什么!)。

我想将其修复为纵向模式,就像应用程序的其他部分一样。

我们怎样才能做到这一点?

Sye*_*bas 5

创建继承自 AVPlayerViewController 的新类并实现支持的接口方法,如下所示。之后,只需创建您创建的类的对象,而不是初始化 AVPlayerViewController 的对象。

\n\n
class MyPortraitVideoController: AVPlayerViewController {\n\n\n    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {\n        return .portrait\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n


\n编辑添加

\n\n

请注意,文档AVPlayerViewController说明:

\n\n
\n

重要

不要子类化 AVPlayerViewController。不支持重写此\n class\xe2\x80\x99s 方法,并会导致未定义的行为。

\n
\n\n

由于这是公认的答案,我认为OP已经尝试过并且它有效,但我认为我应该在这里添加一个注释,以确保未来的读者意识到任何潜在的问题。未定义的行为意味着这可能会在 iOS 的未来版本中停止工作。

\n