You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在App启动时根据设备类型(iPhone/iPad)加载对应Storyboard?

解决方案

你的方向完全正确,通过UIScreen.main.traitCollection.userInterfaceIdiom检测设备类型是当前iOS版本中适配不同设备的标准方式,比旧的设备型号判断更灵活可靠。

完整代码实现(AppDelegate 场景)

你只需要在对应的case分支中,从选中的Storyboard获取初始视图控制器,然后设置为window的根视图控制器即可,完整代码如下:

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 targetStoryboard: UIStoryboard
        switch UIScreen.main.traitCollection.userInterfaceIdiom {
        case .phone:
            targetStoryboard = iPhoneStoryboard
        case .pad:
            targetStoryboard = iPadStoryboard
        @unknown default:
            // 未知设备类型,默认用iPhone版
            targetStoryboard = iPhoneStoryboard
        }
        
        // 获取Storyboard的初始视图控制器
        guard let rootVC = targetStoryboard.instantiateInitialViewController() else {
            fatalError("Storyboard未设置初始视图控制器")
        }
        
        // 创建并配置window
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = rootVC
        window?.makeKeyAndVisible()
        
        return true
    }
}

iOS 13+ SceneDelegate 场景(多数现代项目)

如果你的项目是iOS 13及以上版本,使用SceneDelegate管理应用场景,需要把逻辑移到scene(_:willConnectTo:options:)方法中:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        
        let iPhoneStoryboard = UIStoryboard(name: "IPhone", bundle: nil)
        let iPadStoryboard = UIStoryboard(name: "IPad", bundle: nil)
        
        let targetStoryboard: UIStoryboard
        switch windowScene.traitCollection.userInterfaceIdiom {
        case .phone:
            targetStoryboard = iPhoneStoryboard
        case .pad:
            targetStoryboard = iPadStoryboard
        @unknown default:
            targetStoryboard = iPhoneStoryboard
        }
        
        guard let rootVC = targetStoryboard.instantiateInitialViewController() else {
            fatalError("Storyboard未设置初始视图控制器")
        }
        
        window = UIWindow(windowScene: windowScene)
        window?.rootViewController = rootVC
        window?.makeKeyAndVisible()
    }
}

关键注意事项

  • 确保你的两个Storyboard都已勾选Is Initial View Controller(在Storyboard中选中初始控制器,右侧属性面板中设置)
  • Storyboard的名称要和代码中传入的name参数完全一致(iOS文件名区分大小写)
  • @unknown default分支是为了兼容未来可能新增的设备类型,避免编译报错

内容的提问来源于stack exchange,提问作者ravil_ravil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 15:40:23