![](/img/trans.png)
[英]porting iPad app to iPhone using one storyboard with one storyboard
[英]How to call particular storyboard on app launch based on device type (iphone/ipad)?
我有兩個獨立的故事板,分別用於 iPad 和 iPhone,它們具有相同的類、插座等,但布局不同。
我發現我可以使用UIScreen.main.traitCollection.userInterfaceIdiom
檢測應用程序啟動時的設備類型,但現在我需要調用正確的 storyboard。我該怎么做? 我的方向是否正確? 我發現與此問題相關的所有內容就像 8-9 年前的帖子,所以有時我什至不理解語法。 提前致謝!
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let iPhoneStoryboard = UIStoryboard(name: "IPhone", bundle: nil)
let iPadStoryboard = UIStoryboard(name: "IPad", bundle: nil)
let type = UIScreen.main.traitCollection.userInterfaceIdiom
switch type {
case .phone:
// need to call something here
case .pad:
// need to call something here
@unknown default:
fatalError()
}
您必須通過instantiateInitialViewController()
或instantiateViewController(withIdentifier:)
化視圖 controller
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let iPhoneStoryboard = UIStoryboard(name: "IPhone", bundle: nil)
let iPadStoryboard = UIStoryboard(name: "IPad", bundle: nil)
let type = UIScreen.main.traitCollection.userInterfaceIdiom
switch type {
case .phone:
window?.rootViewController = iPhoneStoryboard.instantiateInitialViewController()
case .pad:
window?.rootViewController = iPadStoryboard.instantiateInitialViewController()
@unknown default:
fatalError()
}
window?.makeKeyAndVisible()
}
}
HII 你可以嘗試將 instantiateViewController 設置為 UINavigationController 並將標識符設置為“Ipad”或“Iphone”,並將 storyboard 的第一個 controller 設置為 UINavigationController 的 rootviewcontroller 並將初始視圖控制設置為 UINavigationController 就像...
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let iPhoneStoryboard = UIStoryboard(name: "IPhone", bundle: nil)
let iPadStoryboard = UIStoryboard(name: "IPad", bundle: nil)
let type = UIScreen.main.traitCollection.userInterfaceIdiom
switch type {
case .phone:
if let IPhone = iPhoneStoryboard.instantiateViewController(withIdentifier: "IPhone") as? UINavigationController {
self.window?.rootViewController = IPhone
}
case .pad:
if let IPad = iPadStoryboard.instantiateViewController(withIdentifier: "IPad") as? UINavigationController {
self.window?.rootViewController = IPad
}
@unknown default:
fatalError()
}
self.window?.makeKeyAndVisible()
self.window?.makeKey()
return true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.