Navigation Bar Items disappearing while using MMDrawerController - ios

I am using MMDrawerController to navigate through various views in my app one of which is a TabBarController which has 5 further views linked to each of its 5 bar items. Each of those 5 views have navigation bar items attached to them. The left bar Item on each of these 5 views toggles the LeftViewController (The view that I use as my drawer). I am using this code in my AppDelegate to achieve this
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarController") as! TabBarController
var leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
var leftsideNav = UINavigationController(rootViewController: leftViewController)
var centerNav = UINavigationController(rootViewController: centerViewController)
centerContainer = MMDrawerController(centerViewController: centerViewController, leftDrawerViewController: leftViewController)
centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView
and I am calling this method when the left bar buttons are tapped on the 5 ViewControllers
#IBAction func leftButtonTapped(sender: AnyObject) {
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
}
Works perfectly so far.
Now I have a tableView in my LeftViewController in which I wish to achieve the following behaviour - clicking on the first row navigates us to the TabBarController while the second row takes us to another ViewController and with withFullCloseAnimation set to true.
Here is the code in the didSelectRowAtIndexPath method of the LeftViewController's tableView
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarController") as! TabBarController
var centerNav = UINavigationController(rootViewController: centerViewController)
appDelegate.centerContainer?.setCenterViewController(centerNav, withFullCloseAnimation: true, completion: nil)
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
This makes the navigation bar items of the 5 views in the TabBarController disappear. It works fine if I use this -
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
The last bit of code makes me think it is initiating the TabBarController all over again rather than 'restoring' the one that had already been initalised. Is that so?
What should be the right way to achieve this?
Sorry for the length of the question (just increased it further by typing this)

I added .navigationController
in the didSelectRowAtIndexPath indexPath: NSIndexPath)
appDelegate.centerContainer!.centerViewController centerViewController.navigationController
break;

Related

ios swift cannot dismiss view controller

I am opening new viewcontroller like
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let view_controller1 = storyBoard.instantiateViewController(withIdentifier: "incident_view_id") as! IncidentViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
//self.present(registerViewController, animated: true)
let navigationController = UINavigationController.init(rootViewController: view_controller1)
appDelegate.window?.rootViewController = navigationController
Which works fine,
Now when I press a button I need to close current view and go back previous view, I tired
self.navigationController?.popToRootViewController(animated: true)
But not works, I tried this too
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
But same result.
You are presenting your ViewController in a wrong way. The following line tells the application to no matter what ViewControllers you have opened, just throw it out and assign the UINavigationController what you are creating, as the rootViewController. So the reason your code, the dismiss, is not working because there are no other ViewControllers behind your current one.
appDelegate.window?.rootViewController = navigationController // Wrong
This usually goes into the beginning of the applications, where you define with which UIViewController you want to start your application. I believe you are calling the first code chunk you posted from a UIViewController. So what you need to do instead of creating a new UINavigationController and assigning it to the rootViewController, you just use the UIViewControllers navigation controller to push the next UIViewController.
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let view_controller1 = storyBoard.instantiateViewController(withIdentifier: "incident_view_id") as! IncidentViewController
navigationController?.pushViewController(view_controller1, animated: true)

SWRevealViewController Open second view controller from menu programatically using swift

I am using SWRevealViewController in my app. Everything is fine except one thing.
Suppose there are A,B,C 3 items in my SWRevealViewController Menu. I want to open B item programatically using swift.
UPDATE: CODE
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewController(withIdentifier: "Reveal") as! SWRevealViewController
let destinationController = storyboard.instantiateViewController(withIdentifier: "VehicleList") as! VehicleList
let navigationController = UINavigationController(rootViewController: destinationController)
sw.pushFrontViewController(navigationController, animated: true)
You can have access to the main screen controller using frontViewController property of the SWRevealViewController like that:
let navigationController = revealViewController().frontViewController as? UINavigationController
navigationController?.pushViewController(viewController, animated: false)
You can replace the frontViewController using setFront(_:animated:) function of SWRevealViewController like:
revealViewController()?.setFront(viewController, animated: false)
This is complete solution two my question.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationController = storyboard.instantiateViewController(withIdentifier: "VehicleList") as! VehicleList
let navigationController = revealViewController().frontViewController as? UINavigationController
navigationController?.pushViewController(destinationController, animated: false)

First ViewController from Tabbar cover the safe area(Status bar)

After sucessful signin i am opening Tabbar with follwing code
let mainView = UIStoryboard(name:"Main", bundle: nil)
let tabbar = mainView.instantiateViewController(withIdentifier: "Tabbar") as? Tabbar
tabbar?.modalPresentationStyle = .fullScreen
self.present(tabbar!, animated: true, completion: nil)
Its open Tabbar with first index selected but first ViewController also cover the save area ...
and switching between the TabbarItems make it work fine ...
I am not able to understand why this happening only in one ViewController on first time open ... and how to tackle that.
Also tried following code but did't work ...
self.edgesForExtendedLayout = []
Its not the proper solution its a kind of hack ... I told in Question that its get automatically fixed after switching between TabbarItems ... so i just added Two lines right after opening Tabbar VC.
tabbar?.selectedIndex = 1
tabbar?.selectedIndex = 0
And know the complete code seems like this.
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let mainView = UIStoryboard(name:"Main", bundle: nil)
let tabbar = mainView.instantiateViewController(withIdentifier: "Tabbar") as? Tabbar
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
appDelegate.window!.rootViewController = tabbar
appDelegate.window!.makeKeyAndVisible()
tabbar?.modalPresentationStyle = .fullScreen
self.present(tabbar!, animated: true, completion: nil)
tabbar?.selectedIndex = 1
tabbar?.selectedIndex = 0

Load all views related to tab bar controller by hard code

I would like to jump from a viewController to the first viewController related to Tab Bar Controller through code.
The tabBarController Scene has storyboard id tabView.
I'm working on this way:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UITabBarController!
storyboard.instantiateViewController(withIdentifier: "tabView")
vc=storyboard.instantiateViewController(withIdentifier: "tabView") as! UITabBarController
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.present(vc as! UIViewController, animated: true, completion: nil)
}
But it loads only the first viewController (out of 5) without the tab bar related to. How can I solve it?
Oh, this code looks so wrong.
In your storyboard give and "tabView" ID to the TabBarController, not the ViewController inside it.
Why you are double instantiating ViewController? just do it once and assign it to the vc variable.
Why you've created delay before presenting the VC? It's some sort of workaround of something?
Working code:
let vc = storyboard.instantiateViewController(withIdentifier: "tabView") as! UITabBarController
self.present(vc, animated: true)
Use
(1)if you want to navigate from Appdelegate
let appDelegate = UIApplication.sharedApplication.delegate as! AppDelegate
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBar = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarController") as! TabBarController
appDelegate.window?.rootViewController = tabBar
appDelegate.window?.makeKeyAndVisible()
(2)if you want to navigate from viewcontroller which has root of navigation
self.navigationController?.pushViewController(tabBar, animated: true)

Tab Bar does not show on View Controller inside Navigation Controller

I'm running into this issue when I'm opening a new View Controller programmatically.
let controller = self.storyboard?.instantiateViewController(withIdentifier: "overViewScreen") as! OverviewViewController
controller.user = self.userObject
let navigationController = UINavigationController(rootViewController: controller)
self.present(navigationController, animated: true, completion: nil)
The structure of my project :
storyboard
On my storyboard the tab bar is shown onto the View Controller (with the table on the right), but when I run the app it looks like this :
enter image description here
I hope you guys can help me out!
Thank you.
You are presenting NavigationController without tab bar controller. You need to present TabBarController.
Give your TabBarController identifier and instantiate them just like you've done with controller
code from comment:
let tabVC = UIStoryboard(name: "NameOfYourStoryboard", bundle: Bundle.main).instantiateInitialViewController() as! UITabBarController
let navVc = tabVC.viewControllers.first as! UINavigationController
let vc = navVc.viewControllers.first as! OverviewViewController
vc.incorrectAuthorization = SettingsAuthorizationMethod.fingerprint
vc.user = self.userObject
present(navController, animated: true, completion: nil)
Ok, I managed to fix it like this :
let vc = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
vc.user = self.userObject
let nvc = UINavigationController(rootViewController: vc)
self.present(nvc, animated: true, completion: nil)
I made a seperate controller class "TabBarController", and added a property "user" to this class. In my "OverViewController" I can get the property as follows :
let tabBar: TabBarController = tabBarController as! TabBarController
user = tabBar.user
Thanks for the help!

Resources