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
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")
}
}
}
来源:https://stackoverflow.com/questions/46645800/force-orientation-not-working-in-ios-10-devices