ios - 仅纵向应用程序中的 AVPlayerViewController 全屏旋转行为

标签 ios iphone swift xcode

当将 AVPlayerViewController 嵌入仅竖屏 iOS 应用程序时,如果在设备处于横向时全屏播放视频,则当播放器退出全屏时,应用程序似乎会卡在奇怪的布局中。

这是错误还是我做错了什么?

此处介绍了如何使用 Xcode 9.4.1、swift 4、iOS 11.4、模拟器或物理设备重现干净的项目。

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    //Create the player and add as child view controller
    let playerVC = AVPlayerViewController()
    self.addChildViewController(playerVC)

    //Place player's view in self
    playerVC.view.frame = CGRect(x: 10, y: 40, width: 355, height: 200)
    self.view.addSubview(playerVC.view)

    //Load example video
    playerVC.player = AVPlayer(url: URL(string: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")!)
}

它的正常工作方式:

  • 播放视频,全屏
  • 旋转为横向,视频旋转
  • 关闭全屏,无论屏幕或设备方向如何,应用都会返回纵向
  • 例如:https://imgur.com/a/MPFmzyH

它是如何 splinter 的:

  • 播放视频,将设备旋转到横向(屏幕不旋转)
  • 点击全屏
  • 退出全屏
  • 屏幕中断,旋转不修复
  • 例如:https://imgur.com/a/hDdmu20

最佳答案

当您离开播放器全屏时,您将进入 View Controller 的 viewWillAppear。所以在 viewWillAppear 中尝试将窗口框架设置为等于屏幕边界。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    appDelegate.window?.rootViewController?.view.frame = UIScreen.main.bounds
}

关于ios - 仅纵向应用程序中的 AVPlayerViewController 全屏旋转行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50977823/

相关文章:

ios - 在不同类的 UIImageView 中更改图像

iphone - 如何从另一个网站(如 osx 仪表板)创建功能的 UIWebView

cocoa - Xcode 6.1 中新的基于文档的 OS X 应用程序模板

ios - iphone App 中字符串的幻灯片放映

iOS:在 uicollectionview 中添加 UIProgressView 以显示上传到 firebase 存储的上传百分比

iphone - 具有透明背景的 ViewController 通过推送转换进入当前 ViewController

ios - 在 UITableViewCell 中加载 React Native View (RCTRootView)

ios - 使用计时器更改 UILabel 的文本

ios - 如何在布局类似于 fork 的 UIViewController 之间导航和传递数据?

iphone - 导入sqlite3.h错误: include of non-modular header inside framework module