我們終於用下面的方式固定它...
我們已經採取了一個Bool
變量上APPD elegate檢測方向變化。
var isLandScapeManualCheck = Bool()
接下來,我們已經實現以下應用委託
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if isLandScapeManualCheck == false{
return UIInterfaceOrientationMask.portrait
}else{
return UIInterfaceOrientationMask.landscapeRight
}
// return UIInterfaceOrientationMask.portrait
}
基於我們回到了方向模式布爾值。
iOS的10及以下版本應該遵循這樣..
在您的PlayerController視圖。(意思是你的家控制器)
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
}
}
}
}
}
後的iOS 11
你應該viewDidLayoutSubviews
調用
UIViewController.attemptRotationToDeviceOrientation()
因此,最後你的子類代碼像belo瓦特
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")
}
}
}
'contentOverlayView .bounds == UIScreen.main.bounds'爲真在IOS 10(及以下)或問題是內部'UIDevice.current.setValue(值,forKey: 「取向」)?' ? –
@AlbertoCantallops裏面的條件,但強制方向代碼不工作 – karthikeyan
看看https://stackoverflow.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8有一堆不同的方法。注意接受的答案是你是如何做到的,但https://stackoverflow.com/a/28220616/7064692也許可以幫助 –