If you set AVPlayerViewController.showsPlaybackControls to false, the controls will not show at all. Even if you tap the screen.
I want the controls to start out hidden, but still be able to summon them by tapping. If I set the mentioned property to true, they start out visible. (Yes they fade after a few seconds.) Is there a way to start hidden, but still be accessible?
UPDATE: I ended up making my own controls for better customization. It's more difficult but worth the time. Please read Apple's sample code for reference. It's about implementing PiP but also about making custom controls: https://developer.apple.com/library/prerelease/ios/samplecode/AVFoundationPiPPlayer/Introduction/Intro.html
UPDATE: When tapped, AVPlayerViewController only fires touchesBegan event, and not touchesEnded event. But it's enough to show the controls.
First you need to hide the control. Put this code right before you present AVPlayerViewController
Then subclass AVPlayerViewController and add this function:
OLD SOLLUTION:
I've just solved this. The main idea is to put a UIView on top of the AVPlayerViewController to reveive tap gesture, and hide that UIView when it is no longer needed.
Here's the code:
And when you need to hide the controls, do this:
thegathering's answer is good. I would override touchesCancelled instead so that the controls do not show and then hide again.
A simple way to do it in Swift 3 is to set
myController.showsPlaybackControls = false
, and to overlay the whole player view with a button or gesture recognizer. I embed it into another view in another controller on a storyboard to make this simple and to not override the player controller. The trick then is to hide the button after being clicked once, because the player controller will thereafter track taps to show/hide the controls.I think I've solved this using dynamic gesture recognizer relationships. The solution avoids custom controls (for consistency), uses only public API and does not subclass
AVPlayerViewController
(which is explicitly disallowed, as noted in other answers).Here's how:
Make a container view controller that embeds
AVPlayerViewController
. (This is useful regardless of the controls, because you need to put the playback logic somewhere.)Set
showsPlaybackControls
tofalse
initially.Add a UITapGestureRecognizer to recognize the initial tap.
In the action method for the gesture recognizer, set
showsPlaybackControls
totrue
.So far, it would work, but the controls would disappear immediately on that initial tap. To fix that, set yourself as a delegate for the gesture recognizer, implement
gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:
and returntrue
for any other single-tap gesture recognizer.Here's the actual implementation in Swift; check andreyvit/ModalMoviePlayerViewController repo for the latest code: