Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayerViewController full screen rotation behavior in portrait only app

When embedding an AVPlayerViewController in a portrait only iOS app it seems the app can get stuck in a weird layout when the player exits full screen if the video is full-screened while the device is held in a landscape orientation.

Is this a bug or am I doing something incorrectly?

Here's how to reproduce with a clean project using Xcode 9.4.1, swift 4, iOS 11.4, simulator or physical device.

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")!)
}

How it works normally:

  • Play video, hit full screen
  • Rotate to landscape, video rotates
  • Close full screen, app returns to portrait regardless of screen or device orientation
  • ex: https://imgur.com/a/MPFmzyH

How it breaks:

  • Play video, rotate device to landscape (screen does not rotate)
  • Hit full screen
  • Exit full screen
  • Screen breaks, rotating does not fix
  • ex: https://imgur.com/a/hDdmu20
like image 390
toemat Avatar asked Jun 21 '18 21:06

toemat


1 Answers

When you leave player full screen you enter in viewWillApper of your View Controller. So in viewWillAppear try to set your window frame to be equal to your screen bounds.

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
}
like image 152
Ivan Georgiev Avatar answered Oct 14 '22 09:10

Ivan Georgiev