iOS - Linking Framework Storyboard to Viewcontroller for Use in Main Project

iOS - Linking framework storyboard to ViewController for use in main project

To get it to find the view controller associated, all you need is -all_load -ObjC in your apps Other Linker Flags in Build Settings.

it seems the linker leaves out the files necessary due to the apps code not actually using or referencing the ViewController, so the linker just leaves it out. This forces it to use the ViewController when linking now. (have a look at this answer for more detail)

i did this with just having my frameworks storyboard in a bundle along side my framework, i followed this guide for finding out how to make the bundle for the framework

Create iOS custom framework using storyboard and viewcontroller

You can reference a storyboard within your framework. You need to reference your framework's bundle specifically to get at the storyboard.

let bundle = Bundle(identifier: "com.custom.framework.bundle.id")
let storyboard = UIStoryboard(name: "NameOfStoryboard", bundle: bundle)

If the view/views are not complex in your storyboard, consider creating xib and a corresponding UIView file.

Is there any way to use storyboard and SwiftUI in same iOS Xcode project?

I just started to look at the SwiftUI. Sharing a small example.

  1. In the storyboard add Hosting View Controller
  2. Subclass the UIHostingController with your own class (ChildHostingController)
    Sample Image
  3. ChildHostingController should look something like that:

import UIKit
import SwiftUI

struct SecondView: View {
var body: some View {
VStack {
Text("Second View").font(.system(size: 36))
Text("Loaded by SecondView").font(.system(size: 14))
}
}
}

class ChildHostingController: UIHostingController<SecondView> {

required init?(coder: NSCoder) {
super.init(coder: coder,rootView: SecondView());
}

override func viewDidLoad() {
super.viewDidLoad()
}
}

For more details have a look at Custom UIHostingController

Apple Docs UIhostingController (Unfortunatelly it hasn't been documented yet)

Integrating SwiftUI Video

Xcode project use main interface from dynamic framework

I have found the problem to my issue, just answering here because it may be useful to someone else.

At the beginning my storyboard and my assets were all in my project, therefore I had no problem to load/link them. But when I moved them into a dynamic library (Cocoa framework touch) my app started crashing and it was because of the way that I was loading the assets that weren't any more in my project but in my framework.
So when I had everything in my project I was doing this:

[UIImage imageNamed:@"bar_grey_2.png"]

But after moving it to my framework I had to updated to:

[UIImage imageNamed:[[NSBundle bundleForClass:[self class]] pathForResource:@"bar_grey_1" ofType:@".png" inDirectory:nil]]

Jut a comment about dynamic frameworks, they are not supported in iOS7 and apple will refuse to accept any app aiming iOS7 with dynamic frameworks.

Accessing storyboard from custom framework not working

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)

Loading a storyboard from a framework

Found the problem. Just silly but after deleting the Main.storyboard from the project, I didn't remove it from the project's settings. Trying to load it as the initial view controller. After changing the settings, UIStoryboard(name:"Main", bundle:NSBundle(forClass:self) just works properly.



Related Topics



Leave a reply



Submit