简体   繁体   中英

iOS: Handle Orientation change without enabling landscape mode on View Controller

My Swift app's View Controller is fixed to portrait mode at all times, but the View Controller has an embedded AV video player (added as a child view controller) which should switch to fullscreen mode on device orientation change to landscape.

Problem I am facing is, if I don't enable landscape mode on the View Controller, I am unable to detect orientation changes (tried with NSNotification.Name.UIApplicationDidChangeStatusBarOrientation , NSNotification.Name.UIDeviceOrientationDidChange ) & hence unable to trigger player fullscreen.

I want my View Controller to not react to orientation changes, as it causes my auto layout views to stretch and adapt to screen size change, but at the same time I want the orientation change to reach the Child View Controller ie., the video player so it can handle switch between fullscreen mode/inline mode.

if let playerVC = createPlayerViewController() as? (UIViewController & MyEmbedPlayer) {

    // Set up events delegate.
    self.playerViewController = playerVC
    self.playerViewController?.setEventsDelegate(self)

    // PlayerViewController added as child of View Controller
    self.addChildViewController(self.playerViewController!)
}

let managedPlayerView = self.playerViewController?.view
managedPlayerView?.removeFromSuperview()
managedPlayerView?.frame = self.playerView.bounds
managedPlayerView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]

playerView.addSubview(managedPlayerView!)

self.playerViewController?.didMove(toParentViewController: self)
self.playerViewController?.setVideoURL(streamURL)

TL;DR: I do not want my screen elements to react to orientation change, but still want my child view controller to receive orientation change events.

try this code

class MYAVPlayerViewController:AVPlayerViewController{

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .all // or you can replace this landscape
    }
}

call in your controller

 let myAVPlayerViewController = MYAVPlayerViewController()
        myAVPlayerViewController.player = player
        self.present(myAVPlayerViewController, animated: true) { 
            self.player.play()

        }

Note : Please make sure the check just portrait in General -> device orientation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM