2

Currently i am doing force orientation change when AVPlayer fullscreen button pressed.I tried something similar

final class NewMoviePlayerViewController: AVPlayerViewController {
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if contentOverlayView?.bounds  == UIScreen.main.bounds{
            DispatchQueue.main.async {
                let value = UIInterfaceOrientation.landscapeRight.rawValue
                UIDevice.current.setValue(value, forKey: "orientation")
            }
            print("full screen")
        }else{
            DispatchQueue.main.async {
            let value = UIInterfaceOrientation.portrait.rawValue
            UIDevice.current.setValue(value, forKey: "orientation")
            }
            print("half screen")
        }
    }

}

The above code working fine in iOS 11 but the same code not working in iOS 10 and below versions

karthikeyan
  • 3,821
  • 3
  • 22
  • 45
  • `contentOverlayView?.bounds == UIScreen.main.bounds` is true in iOS 10 (and below) or the problem is inside `UIDevice.current.setValue(value, forKey: "orientation")`? – Alberto Cantallops Oct 09 '17 at 13:12
  • @AlbertoCantallops the condition going inside but force orientation code not working – karthikeyan Oct 10 '17 at 05:08
  • take a look https://stackoverflow.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8 there are a bunch of different approaches. Notice the accepted answer is how you did it, but https://stackoverflow.com/a/28220616/7064692 maybe could help – Alberto Cantallops Oct 10 '17 at 06:58
  • thanks for reply, its done – karthikeyan Oct 10 '17 at 08:46

1 Answers1

3

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")
        }

    }

}
karthikeyan
  • 3,821
  • 3
  • 22
  • 45