In this article you will learn about Load New Storyboard at Runtime in Swift.
Loading a new Storyboard at Runtime in Swift can be useful for a variety of reasons, depending on the architecture and requirements of your iOS app.
What Is Storyboard
Storyboard is a graphical tools that allows you to design user interface with drag and drop elements and in easy way. A Storyboard is used to graphically of your apps.
You can uses Storyboard to create scenes, segues, constraints, outlets, and actions with a visual editor.
You may also check our Flutter App development services.
Implementing Load New Storyboard at Runtime
1. Create New Project:
First of all we need to create a new project where you will implement Load New Storyboard at Runtime.
2. Create New Storyboard:
In this steps we need to create a new Storyboard (Storyboardtest).
3. Create Enumerations (enum):
In this steps you need to add below mention code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
enum AppStoryboard: String { case test = "Storyboardtest" var instance: UIStoryboard { return UIStoryboard(name: self.rawValue, bundle: Bundle.main) } func viewController<T: UIViewController>(viewControllerClass: T.Type, function: String = #function, line: Int = #line, file: String = #file) -> T { let storyboardID = (viewControllerClass as UIViewController.Type).storyboardID guard let scene = instance.instantiateViewController(withIdentifier: storyboardID) as? T else { fatalError("ViewController with identifier \(storyboardID), not found in \(self.rawValue) Storyboard.\nFile : \(file) \nLine Number : \(line) \nFunction : \(function)") } return scene } } |
Above code is helpful to navigate screen on new Storyboard Controller.
4. Create a Extension:
In this steps we need to create a route and need to add below mention code inside this extension.
1 2 3 4 5 6 7 |
class var storyboardID: String { return "\(self)" } static func instantiate(fromAppStoryboard appStoryboard: AppStoryboard) -> Self { return appStoryboard.viewController(viewControllerClass: self) } |
5. Create Button Action:
In this step we need to create button action to open a new storyboard.
1 2 3 4 5 |
@IBAction func buttonAction(_ sender: Any) { print("test"); let newVC = TestViewController.instantiate(fromAppStoryboard: .test) self.navigationController?.pushViewController(newVC, animated: true) } |
Output
Load New Storyboard at Runtime
Use of Load New Storyboard at Runtime
Here are some common scenarios where dynamically loading a new Storyboard might be beneficial.
1. Dynamic User Interface
If your app needs to support different user interfaces or themes based on user preferences, device types, or other runtime conditions, having multiple Storyboards can make it easier to manage the switch between different UI layouts.
2. Reducing Complexity
Large Storyboard can become unwieldy and difficult to navigate. By breaking down your app into smaller Storyboard . you can reduce the complexity of each individual file, making it easier for multiple team members to work on different parts of the apps concurrently.
3. Conditional Flow
Loading a new Storyboard can be handy when the flow of your app depends on certain condition or user interaction. For example, you might have different onboarding processes for different type of users or scenarious.
4. Localization
For apps that support multiple languages, you might use different Storyboard for each language to handle localized content and layout.
Conclusion
In this article we have discussed about Load New Storyboard at Runtime.
I hope this blog is helpful to understand this topic.
Thanks for reading this blog. You can also check other blogs from here for more knowledge.