Search code examples
swiftios11avplayerviewcontroller

Disable swipe to close on AVPlayerController


iOS 11 has introduced a swipe to close AVPlayerController. I have app that is aimed at toddlers so the screen is easily swiped causing the video to close. Is there anyway to remove the gesture to close the player?

I have tried adding a gesture override to the AVPlayerController's view but it doesn't work. There is a possible solution on How can I add Swipe Gesture to AVPlayer in swift 3 but there must be a cleaner way


Solution

  • I managed to solve issue. As @Vakas commented, the AVPlayerController shouldn't be subclassed. I had originally subclassed it and presented using a modal segue. This was causing the problem.

    To solve it, I created another view controller that embeds the AVPlayerController in it.

    import UIKit
    import AVKit
    
    class PlayerViewController: UIViewController, AVPlayerViewControllerDelegate {
    
        var videoRecord: Video!
        var presentingController = ""
        var videos = [Video]()
        var presentingPlaylist:  Playlist?
    
        let playerViewController = TFLPlayerController()
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            playerViewController.delegate = self
    
            playerViewController.videoRecord = videoRecord
            playerViewController.videos = self.videos
            playerViewController.allowsPictureInPicturePlayback = false
    
            // Add the original AVPlayerController in here
            self.addChildViewController(playerViewController)
    
            let playerView = playerViewController.view
            playerView?.frame = self.view.bounds
    
            self.view.addSubview(playerView!)
            playerViewController.didMove(toParentViewController: self)
    
        }
    
    }
    

    I basically use this View Controller to pass through the properties such as videos, etc, to the originally subclassed AVPlayerController.