I have a top navigation bar that has a logout button which returns the user back to the login screen and wipes their access token from their keychain.
I am working on adding a slide out menu bar, however, my top navigation bar is not appearing and I can't seem to get it to appear.
I am presenting this view on successful login using the following excerpt of code:
DispatchQueue.main.async {
let homePage =
self.storyboard?.instantiateViewController(withIdentifier:
"HomePageViewController") as! HomePageViewController
self.present(homePage, animated: true)
}
Instead of present HomePageViewController, you have to present UINavigationController of HomePageViewController.
Set storyboard ID for UINavigationController of HomePageViewController. For example, you set storyboard ID for UINavigationController is HomePageNavigation
Replace your code with the code below.
DispatchQueue.main.async {
let homePage =
self.storyboard?.instantiateViewController(withIdentifier:
"HomePageNavigation") as! UINavigationController
self.present(homePage, animated: true)
}
Select UINavigationController of HomePageController on Storyboard
Change Storyboard Id Of UINavigationController.
Related
So I have an if statement that has 2 outcomes, rootViewController becomes loginViewController or mainSearchViewController. The storyboard for mainSearchViewController has an embedded TabViewController and a NavigationViewController. However, by setting the root view controller, I lose access to both the TabViewController and a NavigationViewControllerand its only the mainSearchViewController that is active. Here is my code:
func setRootViewController() {
if Auth.auth().currentUser != nil {
let mainSearchViewController = storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.mainSearchViewController)
view.window?.rootViewController = mainSearchViewController
view.window?.makeKeyAndVisible()
} else {
let loginSearchViewController = storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.loginViewController)
view.window?.rootViewController = loginSearchViewController
view.window?.makeKeyAndVisible()
Lose access to Tab Bar Controller and Nav Controller when making the root view controller the view on the very right
Edit: I have tried to set the rootViewController to the Tab Controller, this allows me to use the tab functionality but I still don't have access to the Nav Controller. Same goes for the Nav Controller, it works when it is the root view controller but there is no Tab Controller functionality
Edit 2: In the viewDidLoad() of the mainSearchViewControllernavigationController?.isNavigationBarHidden = false` and I have the root view controller as the Tab Bar Controller. I can access the tab icons, and I can see the navigation bar, but the navigation functionality when tapping on a cell doesn't work.
Changing rootViewController is likely causing your problem since tabViewController probably doesn't have loginViewController as a tab.
If you need to display loginViewController based on an if statement, it's better practice to show it using something like:
navViewController.pushViewController(loginViewController, animated: true)
Why my Tabbar Show in Another Sidemenu item, here is my code
let navigate = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
navigationController?.pushViewController(navigate, animated: true)
For achieving this you have to add navigation controller before making root view controller from tabbar like shown in image. Then it will shown to your another controller.
In my project i want to present Tabbar on button click, now i have already created tabbar and i give identity name as "tabbar" that i show you in below image
so now i am using below code to call Tab bar controller but i am not getting it.
let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
navigationController?.pushViewController(tabbar, animated: true)
can you guys suggest me what i need to do and which code is useful to me to present Tabbar controller.
For more specification : i added below image in which there is blue button in one viewController i want to present tab bar controller on click of that blue button
Thank you.
Try this and see:
Using Storyboard Segue: Connect your segue as present modally with your action button.
Programatically: Use self of view controller (UIViewController) and present it modally, like this.
if let tabbar = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController) {
self.present(tabbar, animated: true, completion: nil)
}
Here is result:
Use this, you dont have the Navigation controller over there, thats why it won't push that way, instead, you need to use following code:
self.present(tabbar, animated: true, completion: nil)
You should be aware that Apple's HIG (Human Interface Guidelines) say that if you have a tabbed application that should be the root-level navigation for the entire app. you're not supposed to do what you are trying to do from a human interface perspective.
That said, it should be technically possible.
My guess is that you don't have a navigation controller.
Use the debugger to check the value of self.navigationController, or add a print statement:
let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
print("navigationController = \(navigationController)")
print("tabbar = \(tabbar)")
navigationController?.pushViewController(tabbar, animated: true)
If either navigationController or tabbar displays as nil, that's your problem.
Select the viewController from which the button click triggered and Select Editor form xcode menu->Embed In->NavigationController.
then write this in button action
let tabbar: UITabBarController? = (self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
self.navigationController?.pushViewController(tabbar!, animated: true)
According to your images I think that your tabBarController has no back button. Means user cannot go back.
If that is the case you can do this.
let tabBar = self.storyboard?.instantiateViewController(withIdentifier: "tabBar")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = tabBar
I want to send a user to a specific ViewController in my app once a notification is clicked.
I now that I can do something like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
presentedVC?.presentViewController(destinationViewController!, animated: true, completion: nil)
But my app has a tab bar and looks like this
Tab bar
tab1: navigationController -> VC1
tab2: navigationController -> VC2 -> HomeVC
tab: navigationController -> VC3
Each tab has a navigationController as a infront of it.
So how can I send the user to HomeVC? I must first select tab 2 then the navigation controller then push the user tvice:
tab2: navigationController -> VC2 -> HomeVC
And the other problem, if there any way to tell if the user is already in HomeVC? I dont want to send the user to the same VC if his already there.
You must have access to your UITabbarController in you UIApplicationDelegate or wherever you're handling the notification tap.
let tabBar:UITabBarController = self.window?.rootViewController as! UITabBarController //or whatever your way of getting reference is
So first you'll get the reference to UINavigationController in your second tab like this:
let navInTab:UINavigationController = tabBar.viewControllers?[1] as! UINavigationController
Now push your home view at second tab's navigation controller:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("Home") as? HomeViewController
navInTab.pushViewController(destinationViewController!, animated: true)
And finally switch your tab to second to show the just pushed home controller
tabBar.selectedIndex = 1
Keep in mind, this answer assumes that your application has already set the tab bar as the root view controller of application window prior handling the notification tap.
Try something like this:
if let tabBarController = window?.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1 // in your case the second tab
}
The idea is to switch to get the tab bar instance and switch it to your desired tab (where you have your view controller).
The above code works in AppDelegate / you can easily call it anywhere by getting the tabBarController instance.
You can check which tab is selected by user with the method var selectedIndex: Int. You can check which view controller is present like this self.navigationController?.presentingViewController?.presentedViewController. This will solve your problem.
This is my storyboard:
Short Description:
My App starts with the LaunchController
a modal segue shows the Reveal View Controller
this bring the Menu Controller and my Main Navigation Controller (ID
"NavController"; green Navbar) together. this will create a slide
menu. (Basic Code:
appcoda.com/ios-programming-sidebar-navigation-menu/)
my Main Navigation Controller shows a TableViewController.
this one have a menu button (3 Lines) which make the slide menu
visible
the plus icon willo push the last view Controller (ID: "VC1").
My Problem:
I would like to set quick actions for my app.
This code help works for that:
#available(iOS 9.0, *)
func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
var succeeded = false
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("NavController")
self.window?.rootViewController = initialViewController
let navVC = self.window?.rootViewController as! UINavigationController
switch shortcutItem.type {
default:
navVC.pushViewController(storyboard.instantiateViewControllerWithIdentifier("VC1"), animated: false)
succeeded = true
break
}
return succeeded
}
This Code set the NavController as initial Controller und push the to VC1.
This works fine.
With the X icon in VC 1, i use the unwind function back to TableView.
The Problem: if i use the undwind function and fall back to tableview i can't open the slide menu. A Touch on the menu icon give no reaction.
The Problem can be, that i start the app with the quick action behind the Reveal View Controller.
How can I solve this problem?
Did you try to make the reveal view controller the entry point of your app and deleting the launch controller, because if you just want to go back to the main view you could just do this :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController() as UIViewController!
self.presentViewController(controller, animated: false, completion: nil)
If you cannot could you explain the purpose of the launch controller?