我的應用適用于 iPad 和 iPhone。由于尺寸不同,設備之間的布局完全不同,我選擇了兩個不同的故事板。
除了我自己的 iPhone SE(第 2 代)之外,一切都可以在模擬器和真實設備上運行,但它確實適用于我妻子的 iPhone SE(第 2 代)。起初,我以為這只是我的設備,但本周我最大的客戶之一也遇到了同樣的問題。他使用的是 iPhone 11。
在我的 SceneDelegate 中,我使用以下代碼檢查用于選擇正確情節提要的設備:
var storyboard: UIStoryboard {
switch UIDevice.current.userInterfaceIdiom {
case .phone:
print("This is an iphone")
return UIStoryboard(name: "Main-iPhone", bundle: nil)
case .pad:
print("This is an ipad")
return UIStoryboard(name: "Main-iPad", bundle: nil)
default:
print("This is default")
return UIStoryboard(name: "Main", bundle: nil)
}
}
window?.rootViewController = storyboard.instantiateInitialViewController()
window?.makeKeyAndVisible()
guard let _ = (scene as? UIWindowScene) else { return }
我通過列印陳述句檢查選擇。這里一切都還很順利。當涉及到 iPhone 時,我在 ViewController 中使用相同的 switch 陳述句來觸發不同的行為。從未知的 UI 連接元素時,應用程式崩潰。
事實證明,加載的是 iPad 的情節提要而不是 iPhone 的情節提要。即使我給每個故事板一個 ViewController,我設備上的應用程式仍會繼續加載錯誤的故事板。
我已經多次從我的設備中洗掉了該應用程式。打開和關閉設備。故事板被洗掉并再次添加。一個構建檔案夾干凈。洗掉派生資料。但沒有任何幫助。
有沒有人認識到這個問題?
非常感謝
uj5u.com熱心網友回復:
嗯……
首先,確保您已經洗掉了info.plist.
按原樣使用您的代碼,我看不到這些print()陳述句...
嘗試將其更改為:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
var storyboard: UIStoryboard {
switch UIDevice.current.userInterfaceIdiom {
case .phone:
print("This is an iphone")
return UIStoryboard(name: "Main-iPhone", bundle: nil)
case .pad:
print("This is an ipad")
return UIStoryboard(name: "Main-iPad", bundle: nil)
default:
print("This is default")
return UIStoryboard(name: "Main", bundle: nil)
}
}
window?.rootViewController = storyboard.instantiateInitialViewController()
window?.makeKeyAndVisible()
}
uj5u.com熱心網友回復:
您在部署資訊配置中設定的主界面是哪個界面?如果問題僅發生在手機上,您可以嘗試設定Main-iPhone為主界面并僅在 iPad 中覆寫它SceneDelegate

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/408662.html
標籤:
