skip to Main Content

I have two separate storyboards for iPad and iPhone, they have the same classes, outlets and etc, but different layouts.

I found that I can detect device type on app launch with UIScreen.main.traitCollection.userInterfaceIdiom, but now I need to call correct storyboard. How do I do that? Am I even on the right direction? All I found related to this problem is like posts made 8-9 years ago so I don’t even understand syntax sometimes.
Thanks in advance!

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()
        }

2

Answers


  1. You have to instantiate the view controller by instantiateInitialViewController() or instantiateViewController(withIdentifier:)

    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()
        }
    }
    
    
    Login or Signup to reply.
  2. HII can you try to set instantiateViewController to UINavigationController and set identifier like as "Ipad" or "Iphone" and set first controller of storyboard to rootviewcontroller of UINavigationController and set initial viewcontrolled to UINavigationController like as…

    @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
    
    
    }
            
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search