I created an AVPlayerViewController and an attached AVPlayer in the viewDidAppear method of a custom UIViewController. Ho
I am not sure if this helps your usecase, but if you can target iOS 12+, AVPlayerViewController does provide a delegate property/protocol (AVPlayerViewControllerDelegate) with a few related delegate methods that might help (see below):
Particularly on iOS 12+:
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator)
On tvOS 11+:
func playerViewControllerShouldDismiss(_ playerViewController: AVPlayerViewController) -> Bool
func playerViewControllerWillBeginDismissalTransition(_ playerViewController: AVPlayerViewController)
func playerViewControllerDidEndDismissalTransition(_ playerViewController: AVPlayerViewController)