[英]Locked ViewController orientation breaking when segueing back and forth
我有一個簡單的相機應用程序。 Camera Preview屏幕是一個視圖控制器(名稱為ViewController),我通過此代碼鎖定了方向(找不到此代碼的StackOverflow鏈接),該視圖控制器嵌入在導航控制器中:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let navigationController = self.window?.rootViewController as? UINavigationController {
if navigationController.visibleViewController is ViewController {
return UIInterfaceOrientationMask.portrait
} else {
return UIInterfaceOrientationMask.all
}
}
return UIInterfaceOrientationMask.portrait
}
相機啟動時,“相機預覽”鎖定為.portrait方向,這正是我想要的。 但是,如果我按下圖庫按鈕(將我轉至另一個屏幕),並通過旋轉物理設備來更改圖庫屏幕的方向,然后通過“后退”按鈕返回到“我的相機預覽”屏幕,則該方向被破壞了。 。 它是橫向模式,而不是縱向模式。 如何獲得硬鎖?
以防萬一,我在這里做了一個我的代碼的虛擬模型(太長了,無法在此處發布),你們可以在這里自己測試一下
https://github.com/bigmit2011/Testing-Camera
編輯:
如果應用程序崩潰,請嘗試再運行一次。 由於某種原因,應用程序首次運行時會崩潰。 謝謝。
EDIT2:
嘗試將嵌入式ViewController設置為UINavigationController的委托。 但是,我不太了解我是否正確執行了以下操作:
class ViewController: UIViewController, UINavigationControllerDelegate {
var navigation: UINavigationController?
self.navigation.delegate = self // doesn't seem to work
func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
return .portrait
}
我是否需要為UiNavigationController創建一個單獨的swift文件?
EDIT3:以下Matt的答案代碼如下:
class CameraViewController :UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
self.navigationController?.delegate = self
super.viewDidLoad()
func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
return .portrait
}
但是,這似乎將所有內容都鎖定為縱向模式,而不僅僅是鎖定在Navigation Controller中的單個viewController。
您正在犯兩個錯誤。
首先,您嘗試從應用程序委托中控制各個視圖控制器的方向。 錯了 應用程序委托人控制整個應用程序的可能方向。 但是對於視圖控制器,每個單獨的視圖控制器都控制自己的方向。 只需在兩個視圖控制器(而不是應用程序委托)中的每一個中實現supportedInterfaceOrientations
。
其次,您的第一個視圖控制器在導航界面中。 因此,它不是頂級視圖控制器,並且不能直接管理方向(使用supportedInterfaceOrientations
)。 控制導航界面方向的正確方法是將自己設置為導航控制器的委托,並實現navigationControllerSupportedInterfaceOrientations(_:)
。
我也無法運行您的應用。 但是我用主VC,第二VC在Xcode中對此進行了測試,並將主VC嵌入到導航控制器中。 然后,我按照本文中的建議在iOS中進行了選擇。
您將以下內容添加到AppDelegate:
var enableAllOrientation = false
func application(_ application: UIApplication,supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if (enableAllOrientation == true){
return UIInterfaceOrientationMask.allButUpsideDown
}
return UIInterfaceOrientationMask.portrait
}
然后在允許allButUpsideDown的VC中(或將其更改為所需的內容),添加以下代碼:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.enableAllOrientation = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.enableAllOrientation = false
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
我將此放在第二個VC中。 在模擬器中運行該應用程序,將其鎖定到第二個VC,將方向更改為橫向,然后回擊,然后將主VC鎖定為縱向模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.