首页 文章

iOS 8 - 在解除全屏AVPlayerViewController时将方向改回肖像

提问于
浏览
14

我的应用程序是一个仅限肖像的应用程序,但我有一个视图控制器,它使用AVPlayerViewController显示实时流 .

为了允许横向播放该播放器的全屏视图,我在 AppDelegate.swift 中编写了此方法:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    var orientation =  UIInterfaceOrientationMask.Portrait

    if let presentedController = window?.rootViewController?.presentedViewController? {

        if presentedController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) {
            orientation = .AllButUpsideDown
        } else if let navController = presentedController as? UINavigationController {
            if navController.topViewController.isKindOfClass( NSClassFromString("AVFullScreenViewController").self ) {
               orientation = .AllButUpsideDown
            }
        }
    }

    return  Int(orientation.rawValue)
}

这就是我调用初始化我的AVPlayer的方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showLiveStream" {

        SVProgressHUD.show()
        var queue: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

        dispatch_async(queue, {
            let streamURL = NSURL(string: liveStreamURL)
            let playerItem = AVPlayerItem(URL: streamURL)
            let player = AVPlayer(playerItem: playerItem)

            dispatch_async(dispatch_get_main_queue(), {
                SVProgressHUD.dismiss()
                var playerViewController = segue.destinationViewController as AVPlayerViewController
                playerViewController.player = player
            })
        })
    }
}

问题:当我打开播放器的全屏视图,然后更改为横向,然后单击“完成”以关闭全屏视图,我的应用程序保持在横向 . 但我希望它再次旋转成肖像 . 我怎样才能做到这一点?

3 回答

  • 0

    而不是实现 application(_:supportedInterfaceOrientationsForWindow:) 尝试在每个视图控制器上实现 supportedInterfaceOrientations() . 例如:

    override func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }
    

    这将确保视图控制器无法以横向显示,因此在关闭视频播放器时,它将直接返回纵向窗口 .

    Update Objective-C:

    - (NSUInteger)supportedInterfaceOrientations { 
        return UIInterfaceOrientationMaskPortrait; 
    }
    
  • 0

    Add These Lines in Appdelegate and your Required View Controller

    -(BOOL)shouldAutorotate
    {
        return NO; 
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        //LandScapeMode:- UIInterfaceOrientationMaskLandscape;
        //PortraitMode:- 
        return UIInterfaceOrientationMaskPortrait 
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        //LandScapeMode:- UIInterfaceOrientationLandscapeRight;
       // ProtraitMode:-
       return UIInterfaceOrientationPortrait
    }
    
  • 0

    我做了什么来解决这个问题,创建一个带有'viewDidLayoutSubviews'功能的新类并覆盖它!

    final class NewMoviePlayerViewController: AVPlayerViewController {
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
    
            if view.bounds == contentOverlayView?.bounds {
                let value = UIInterfaceOrientation.portrait.rawValue
                UIDevice.current.setValue(value, forKey: "orientation")
            }
        }
    }
    

相关问题