Force orientation not working in iOS 10 devices

岁酱吖の 提交于 2019-12-06 12:59:05

We have finally fixed it with following way...

We have taken one Bool variable on AppDelegate to detect orientation change.

var isLandScapeManualCheck = Bool()

Next we have implemented following application delegate

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

        if isLandScapeManualCheck == false{
        return UIInterfaceOrientationMask.portrait
        }else{

            return UIInterfaceOrientationMask.landscapeRight
        }
//        return UIInterfaceOrientationMask.portrait

    }

Based on bool value we have return out orientation mode.

iOS 10 and below version should follow this way..

In your playercontroller view..(mean your home controller)

if #available(iOS 11, *) {

                 }else{
                   playerVwController.contentOverlayView!.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.new, context: nil)
                }



override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "bounds"{
            let rect = change![.newKey] as! NSValue

            if let playerRect: CGRect = rect.cgRectValue as CGRect {
                if playerRect.size == UIScreen.main.bounds.size {
                    print("Player in full screen")
                    let value =  UIInterfaceOrientation.landscapeLeft.rawValue
                    UIDevice.current.setValue(value, forKey: "orientation")
                    isLandScapeManualCheck = true
                } else {
                    DispatchQueue.main.async {
                    let value =  UIInterfaceOrientation.portrait.rawValue
                    UIDevice.current.setValue(value, forKey: "orientation")
                    print("Player not in full screen")
                    isLandScapeManualCheck = false
                    }

                }
            }
        }
    }

After iOS 11

You should call in viewDidLayoutSubviews

    UIViewController.attemptRotationToDeviceOrientation()

So finally your sub class code like below

final class NewMoviePlayerViewController: AVPlayerViewController {
    override func viewDidLayoutSubviews() {
        UIViewController.attemptRotationToDeviceOrientation()
        super.viewDidLayoutSubviews()
        if contentOverlayView?.bounds  == UIScreen.main.bounds{
            DispatchQueue.main.async {
                let value = UIInterfaceOrientation.landscapeRight.rawValue
                UIDevice.current.setValue(value, forKey: "orientation")
            }
//            self.contentOverlayView?.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
            print("full screen")
        }else{
            DispatchQueue.main.async {
            let value = UIInterfaceOrientation.portrait.rawValue
            UIDevice.current.setValue(value, forKey: "orientation")
            }
//            self.contentOverlayView?.transform = CGAffineTransform.identity

            print("half screen")
        }

    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!