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
Related
So I can't use initial view controller for the tab bar controller because there is already one and this is way further along in the app. That is why I made a tab bar controller programmatically inside the first to two view controllers that I want in the tab bar controller.
Here is the VC where I add the tab bar controller to, in viewDidLoad.
let tabBarController2 = YourTabBarController()
addChild(tabBarController2)
view.addSubview(tabBarController2.view)
NSLayoutConstraint.activate([
tabBarController2.view.bottomAnchor.constraint(equalTo: mainview.bottomAnchor),
tabBarController2.view.leadingAnchor.constraint(equalTo: mainview.leadingAnchor),
tabBarController2.view.trailingAnchor.constraint(equalTo: mainview.trailingAnchor),
])
tabBarController2
.didMove(toParent: self)
below is the class of the tab bar controller
super.viewDidLoad()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstViewController = homepage()
let navigationController = UINavigationController(rootViewController: firstViewController)
navigationController.title = "First"
viewControllers = [navigationController]
if let secondViewController = storyboard.instantiateViewController(withIdentifier: "ranking") as? ranking {
let navgitaionController1 = UINavigationController(rootViewController: secondViewController)
navgitaionController1.title = "Second"
var array = self.viewControllers
array?.append(navgitaionController1)
self.viewControllers = array
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 have some code to only show the first view controller in my storyboard on the first launch of the app. After that I want to skip that page and go straight to my second view on each launch. I have embedded the first view (which is connected to the second) in a navigation controller.
My issue is that after the first launch when the app goes to the second view directly it's showing the view without the navigation bar on top and I'm not sure why.
In my appdelegate:
func firstLaunchCheck(){
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialView : UIViewController = storyboard.instantiateViewController(withIdentifier: "mainScreen") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialView
self.window?.makeKeyAndVisible()
}
else{
UserDefaults.standard.set(true, forKey: "launchedBefore")
}
}
UPDATE:
I wound up just changing which view controller were embedded in the navigation controller (excluded the first one) since it didn't make sense to me to have it there. So now after the first launch it loads the navigation controller
SecondViewController is not added in UINavigationController hierarchy, to see the navigationBar on top you can push SecondViewController on firstVC if the launchedBefore is false in appDelegate
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
let navigationController = window.rootViewController as! UINavigationController
navigationController?.pushViewController(secondVC, animated: false)
You need to embed the second view controller i.e. "mainScreen" in UINavigationController and then make it the rootViewController of your app window.
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = UINavigationController.init(rootViewController: storyboard.instantiateViewController(withIdentifier: "mainScreen"))
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialView
self.window?.makeKeyAndVisible()
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)
I have a tab controller on my iOS App and I want to check some conditions first and do as follow:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if condition {
globalClass.token = u
let mvc: MainViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("mainView") as! MainViewController
self.window?.rootViewController = mvc
} else {
globalClass.token = ""
let mvc: LoginViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
self.window?.rootViewController = mvc
}
self.window?.makeKeyAndVisible()
My problem is that when I programmatically set the initial view controller to first tab of the tab controller when it is loaded, the tab menus at bottom won't load. It just loads the view controller not tab menu.
MainViewController is the first tab of the tab view controller
Thanks,
Afshin
Try this code.
let tbc = mainStoryBoard.instantiateViewControllerWithIdentifier("tabbarStoryboardId") as! UItabBarController
self.window?.rootViewController = tbc
Thanks