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:
How it breaks:
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With