Go back to tab bar controller with index 2 - swift

this is my procedural scheme
i have a tab controllore with 3 items and into 1 items i have a navigation controller than into this i have a popup
i need to go back from my popup to my tab bar controller with index 2
this is my code but doesn't work.
let TabViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController
self.present(TabViewController, animated: true)
I have to be able to go back from my popup, to my tab bar controller directly to the page with item 2

You can try
self.dismiss(animated:true) {
let TabViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController
TabViewController.selectedIndex = 2
UIApplication.shared.keyWindow?.rootViewController = TabViewController
}

Related

only one tab at a time showing up

I am unable to get each tab to show up before it is clicked, I tried setting each one to its own view controller then changing the title that way but it's still not working ( sorry if my formatting is off , this is by first post on here)
thanks
#objc func didTapButton(){
// create and present tab bar controller
let tabBarVC = UITabBarController()
let vc1 = UINavigationController(rootViewController: FirstViewController())
let vc2 = UINavigationController(rootViewController: SecondViewController())
let vc3 = UINavigationController(rootViewController: ThirdViewController())
vc1.title = "My Groups"
vc2.title = "My Events"
vc3.title = "Browse Events"
tabBarVC.setViewControllers([vc1, vc2, vc3], animated: false)
guard let items = tabBarVC.tabBar.items else{
return
}
let images = ["bell", "star", "person"]
for x in 0..<items.count{
items[x].image = UIImage(systemName: images[x])
}
tabBarVC.modalPresentationStyle = .fullScreen
present(tabBarVC, animated: true)
}
Screenshot:
From the doc :
Configuring your tab bar programmatically:
To configure the tab bar associated with a UITabBarController object, configure the view controllers associated with the tab bar controller. The tab bar automatically obtains its items from the tabBarItem property of each view controller associated with the tab bar controller.
To configure tab bar items directly, use the setItems(_:animated:) method of the tab bar itself.
You can not use tabBar.items property directly as you do

How can I present inside a tab of a UITabBarViewController?

I want to be able to create a TabBarViewController with some tabs and then push into the given tabs
let tabBarViewController = UITabBarController()
let redVc = UIViewController()
redVc.view.backgroundColor = .red
let blueVc = UIViewController()
blueVc.view.backgroundColor = .blue
tabBarViewController.viewControllers = [redVc, blueVc]
This created a tabBarViewController with a red and a blue tab. Now I want to push a yellow VC to the red tab so that I have a yellow and a blue tab.
let yellowVc = UIViewController()
yellowVc.view.backgroundColor = .yellow
tabBarViewController.modalPresentationStyle = .fullScreen
// this doesn't work
viewController.present(tabBarViewController, animated: true)
// must use this
tabBarViewController.viewControllers![0] = yellowVc
What should I do to be able to present in a given tab?
You can setup your UITabbarController in such a way, that each of it's child is a UINavigationController.
All together, you then have the following hierarchy:
UITabbarController
child1 (UINavigationController)
first content ViewController
child2 (UINavigationController)
second content ViewController
Now from within each contentViewController, you can use navigationcontroller.push to push a new viewController to the stack and it will stay inside the tabbar.

Push View Controller from Tab Bar Controller

My tab bar controller is not embedded in a navigation controller.
On index 1 (which is embedded in a navigation controller) I have an alert that should show a view controller inside index 2 (also embedded in its own navigation controller).
I would like to jump to the view controller which is inside index 2 (called subVC). The following code does not work. navController is coming up nil.
let subVC = self.storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.subscriptionVC) as! SubscriptionViewController
self.selectedIndex = 2
let navController = self.tabBarController?.selectedViewController?.navigationController
navController?.pushViewController(subVC, animated: true)

Present viewcontroller with tabbar

I created UITabBarController programmatically in AppDelegate with 4 view controllers(using .xib). When user tap some button on ViewController (VC-A) it present another VC (VC-B) and covered tabbar. So I want to VC-B has a tabbar on the button.
I tried to add VC-B as a child of tabbarcontroller. I tried to .present(vc) and .show(vc) on both: VC-A and VC-A.TabBarController
Creating controllers in AppDelegate:
let controllers = [tabViewController1,tabViewController2,tabViewController3,tabViewController4]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
presenting in VC-A
self.tabBarController?.present(controller, animated: false, completion: nil)
right click and drag from tabbar controller in storyboard to VC-B. that should create a tab on the bottom of your VC-A and VC-B to go back and forth without having to implement any backend code unless you want to animate
The solution is to embed every VC in navigationController and then add to TabBarController.
let vc1 = ViewController1()
let navController1 = UINavigationController(rootViewController: vc1)
navController.isNavigationBarHidden = true
let controllers = [navController1, navController2, navController3, navController4]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
Then call
self.navigationController?.pushViewController(controller, animated:
true)
To diplay VC with tabbar
I will press the red login button at the bottom of the picture and try to log in.
login button => "로그인"
After that, log in.
let moreVC = self.storyboard?.instantiateViewController(withIdentifier: "MoreViewController") as! MoreViewController
moreVC.definesPresentationContext = true
moreVC.modalPresentationStyle = .fullScreen
let navController = UINavigationController(rootViewController: moreVC)
self.present(navController, animated: true, completion: nil)
If the login proceeds without error, the above code will be called to display the screen when the login is completed.
If the flow proceeds as the code above, this screen appears.
The screen shown is not fullscreen, and the tabbar at the bottom is gone. The screen I want is the screen below.
How can I present a tab bar when presenting the screen?

Tab bar disappeared when back to initial view controller

i have 2 view controllers then tab bar controller connected to 3 navigation controller , each navigation connected to multiple views .
The point that from one of these views that is connected to one of navigation controllers , i want to back again to the start screen , it good by this 2 lines :
let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
UIApplication.shared.keyWindow?.rootViewController = loginViewController
but after i do this and want to navigate again to the tab bar its disappeared from the view . I used this to navigate to tab bar :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let viewTabController = self.storyboard?.instantiateViewController(withIdentifier :"MainTabBarController") as! MainTabBarController
viewTabController.selectedIndex = 1
appDelegate.window?.rootViewController = viewTabController
Thanks in advance.
When you use instantiateViewController you are making new instances of view controllers in memory while there is no need in your case , you have created them before. so you can dissmis your tabBar and go back to first screen like this (initial viewcontroller must be embeded into a navigation controller) :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = appDelegate.window?.rootViewController as! UINavigationController
navigationController.dismiss(animated: true, completion: nil)
for bringing back tabBar you can either save its instance in your app delegate and present it or use a storyboard segue to present it modally
:)