Search code examples
iosswiftstoryboarduistoryboard

Accessing storyboard from custom framework not working


Hi I have to access storyboard from custom framework (LoginUIModule, LoginUIModule have storyboard LoginScreen.storyboard) in app delegate. I removed Main storyboard from Main Interface and also removed name from Main storyboard file base name as well from .plist, but I getting an error

reason: 'Could not find a storyboard named 'LoginScreen' in bundle NSBundle

Note:- LoginUIModule is separate Module and I need to access it in my main (OneAppllbs) project which is a again separate module

The Code which I used in app delegate

import LoginUIModule

 self.window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "LoginScreen", bundle: nil)
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginUIModuleViewController") as? LoginUIModuleViewController
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

Solution

  • You need set Bundle to access Storyboard.

    First create storyboardBundle with Bundle Identifier for framework;

    let storyboardName = "LoginScreen"
    let storyboardBundle = Bundle(for: LoginUIModuleViewController.self)
    

    or referencing a class in framework:

    let storyboardBundle = Bundle(identifier: "com.yourframework.id")
    

    Then create storyboard with this bundle:

    let storyboard = UIStoryboard(name: storyboardName, bundle: storyboardBundle)