Reload storyboard to change language - ios

I am implementing multiple language support in a app. Can any one say how to reload storyboard to change localised text without restart of app?

To reload storyboard you need to set RootViewController on changing language. You can use bellow line of code to set RootViewController:
let firstVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstViewController")
let rootviewcontroller: UIWindow = ((UIApplication.shared.delegate?.window)!)!
rootviewcontroller.rootViewController = firstVC
If you want to embed navigation controller in firstVC you should use following line of code:
let firstVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstViewController")
let nav = UINavigationController(rootViewController: firstVC)
let rootviewcontroller: UIWindow = ((UIApplication.shared.delegate?.window)!)!
rootviewcontroller.rootViewController = nav

Related

How to access to all possible view controllers in a tabBarViewController from storyboard?

I would like to programmatically construct a replica of the storyboard tabBarController, as follows:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarViewController")
viewControllers = tabBarController.childViewControllers
My storyboard tabBarController contains 7 tabs. But the code above only provide me with the 4 firsts (on iPhone). How can I get the others tabs from the storyboard?
The problem is because you are using wrong property to get viewcontrollers. You should use viewControllers instead of childViewControllers. Try the code below, it will return 7 controllers for viewControllers value
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarViewController") as! UITabBarController
let viewControllers = tabBarController.viewControllers;

Go back to initial root ViewController

In my app, I have a Login Controller which is my initial viewController, and when I log into the app, I change my Root ViewController thanks to this code :
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let newVC = storyBoard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.view.window?.rootViewController = newVC
But now, I would like to go back to my Login Page when I click on Disconnect button. The problem is that I don't manage to go to this page. Whatever I try, it reloads the SecondViewController but not the LoginController. Here is what I have tried :
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let projectVC = newVC.instantiateViewControllerWithIdentifier("LoginController") as! LoginController
self.view.window?.rootViewController = newVC
Or
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let newVC = storyBoard.instantiateInitialViewController()
self.view.window?.rootViewController = newVC
Is it possible to go back to the initial viewcontroller ? Or is there a way to just reload my app ?
Thanks for your help :)
The best way to not change rootViewController property on a window is to make some parent view controller as "root" and add/change child view controllers (Login, Main, etc.) to it as needed.

Swift3 ViewController cannot load normally and show black view

I want to create a Welcome & Guide page for an iOS app. I am using Swift 3. My problem is that when I try to show the Main.storyboard after finishing the Guide instructions, I get a black view instead of the Main.storyboard:
I use window?.rootViewController = ViewController()in AppDelegate.swift to switch a new ViewController which is linked with the Main.storyboard. But I get nothing except a black view.
If you have used storyboard then you will have to fetch your view controller as
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "your_identifier_which_you_have_set_in_storyboard")
window?.rootViewController = viewController
If your viewController is embedded in UINavigationController then just make this change:
window?.rootViewController = UINavigationController(rootViewController: viewController)
try this one.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MainVC")
window?.rootViewController = UINavigationController(rootViewController: viewController)

Add UINavigationController to UIView?

I would like to load an UINavigationController inside an UIView (SideView) - do i need a ContainerView?
Ill tried with:
let sideRoot = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("sideRoot") as! SideRootViewController
let navController = UINavigationController(rootViewController: sideRoot)
sideView.addSubview(navController.view) // sideView is a UIView
But ill only see the NavigationBar, but no content.
Do i need an ContainerView? Or is it possible to add a (non fullscreen) UINavigationController?
To add a NavigationController inside your ViewController's custom view, you must add the navigation controller to the view controller and add the view from the navigation controller into your custom view.
Try this code.
let sideRoot = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("sideRoot") as! SideRootViewController
let navController = UINavigationController(rootViewController: sideRoot)
self.addChildViewController(navController)
sideView.addSubview(navController.view)

Load a controller embed to a UINavigationController from AppDelegate in Swift

I need to display from AppDelegate a table view embed to an UINavigationController.
Normally I use
let viewController: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("prova") as ViewController
window?.rootViewController?.presentViewController(viewController, animated: false, completion: nil)
but it doesn't work with embed controller, how need to be adapted this code?
Here is a test project.
You need to instantiate the UINavigationController with its identifier from your storyboard. The navigation controller should be connected to its rootViewController in the storyboard and will automatically show it.
let navController: UINavigationController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("provaNavController") as UINavigationController
window?.rootViewController?.presentViewController(navController, animated: false, completion: nil)
Update
Since you are not setting an initial view controller in the storyboard, use this code:
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let navController: UINavigationController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("provaNavController") as UINavigationController
window?.rootViewController = navController
window?.makeKeyAndVisible()
You also need to clear out Main.storyboard from the general project settings:

Resources