Xcode cannot find referenced Storyboard using Cocoapods

后端 未结 2 1969
孤独总比滥情好
孤独总比滥情好 2021-02-08 03:21

I\'m trying to create a Pod using CocoaPods and I want to bundle a \"Demo\" Storyboard that I can reference to from the Main Storyboard from my example application. The problem

相关标签:
2条回答
  • 2021-02-08 03:33

    Files included in a Dynamic Framework are embedded in a different NSBundle in the application. To make it easier to obtain the storyboard, you can create a helper class inside your library:

    public class StoryboardHelper: NSObject {
        public static let helper = StoryboardHelper()
    
        public lazy var storyboard: UIStoryboard! = UIStoryboard(name: "Main", bundle: NSBundle(forClass: StoryboardHelper.self))
    
        public func rootController() -> UIViewController! {
            return storyboard.instantiateInitialViewController()
        }
    }
    

    NSBundle(forClass: StoryboardHelper.self) will obtain the bundle of StoryboardHelper.

    This way, your user only needs to do the following to show your root view controller of the embedded storyboard:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        window?.rootViewController = StoryboardHelper.helper.rootController()
        window?.makeKeyAndVisible()
    
        return true
    }
    
    0 讨论(0)
  • 2021-02-08 03:57

    To reference storyboard using cocoapods, you should set in the Bundle section, the bundle identifier of the pod, like the picture below:

    And in your podspec file, you should add the reference of your storyboard (as resource and not bundle)

    s.resource = 'MyPod/MyStoryboardName.storyboard'

    After the pod update, everything will work as expected

    Hope that helps

    0 讨论(0)
提交回复
热议问题