Updated 22 February 2024
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.
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.
First of all we need to create a new project where you will implement Load New Storyboard at Runtime.
In this steps we need to create a new Storyboard (Storyboardtest).
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.
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) } |
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) } |
Here are some common scenarios where dynamically loading a new Storyboard might be beneficial.
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.
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.
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.
For apps that support multiple languages, you might use different Storyboard for each language to handle localized content and layout.
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.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.