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)
Related
I am trying to skip the login screen and go straight to the MainViewController when the user is logged in. However the problem is that I have a Tab Bar Controller and Navigation Controller between the login and the main vc. After extensive search I wrote the below code
func showMainViewController() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController: MainViewController = storyboard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
let navigationController = UINavigationController(rootViewController: mainViewController)
//It removes all view controllers from the navigation controller then sets the new root view controller and it pops.
window?.rootViewController = navigationController
// //Navigation bar is hidden
// navigationController.isNavigationBarHidden = true
}
However it fails to show the tab bar view controller. Any help is appreciated.
try this
func showMainViewController() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController: MainViewController = storyboard.instantiateViewController(withIdentifier: "TabBar")
window?.rootViewController = mainViewController
window?.makeKeyAndVisible()
}
you should instantiate TabBar no main viewController. since is instantiate inmediatrly is if first index or set the selected index
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;
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
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)
I have just programatically embedded a tab bar controller into my app, however it seems to have forced my Storyboard embedded Navigation Bars to disappear, I assume this is because I have now set the rootviewController to be my tab bar.
I read the answer to this post as it seemed that the problem was similar, however doing so prompts me with the error Pushing a navigation controller is not supported
Below is my code written in AppDelegate, here I create my tab-view and a navigation controller to push to the root view controller:
// Set up the tab and navigation bar controllers
var currController = window?.rootViewController
let chatSB = UIStoryboard(name: "Chat", bundle: nil)
let mainSB = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = UITabBarController()
var navigationController = UINavigationController(rootViewController: currController!)
let profileVC = mainSB.instantiateViewControllerWithIdentifier("profileVC") as TimelineTableViewController
let chatVC = chatSB.instantiateViewControllerWithIdentifier("chatInboxVC") as ChatInboxViewController
tabBarController.viewControllers = [profileVC, chatVC, navigationController]
window?.rootViewController = tabBarController
How would I go about fixing this issue?
If your desired view controllers are embedded in UINavigationController instances you need to instantiate those rather than the desired view controllers directly. The storyboard will take care of instantiating the embedded view controllers.
So, if your two navigation controller scenes have "profileNavController" and "chatInboxNavController" as their identifiers, your code would be -
// Set up the tab and navigation bar controllers
var currController = window?.rootViewController
let chatSB = UIStoryboard(name: "Chat", bundle: nil)
let mainSB = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = UITabBarController()
var navigationController = UINavigationController(rootViewController: currController!)
let profileNavController = mainSB.instantiateViewControllerWithIdentifier("profileNavController") as UINavigationController
let chatNavController = chatSB.instantiateViewControllerWithIdentifier("chatInboxNavController") as UINavigationController
tabBarController.viewControllers = [profileNavController, chatNavController, navigationController]
window?.rootViewController = tabBarController