How to programatically unload a view controller from Tab bar controller? - ios

I have created a tab bar controller in storyboard, with 5 tab bar items. I want to remove one view controller programatically from the "viewcontrollers" array of the tab bar stack. I also want the app to be show some other tab item as selected when i remove the above view controller. I have tried with the below code, but its not working.
if let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController {
tabBarController.viewControllers?.remove(at: 2)
tabBarController.selectedIndex = 1
}

Reassign viewControllers property without the one you don't want:
if let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController {
tabBarController.selectedIndex = 1
var controllers = tabBarController.viewControllers
controllers.remove(at: 2)
tabBarController.viewControllers = controllers
}
Now this code is ok, but the problem is the following line:
let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController
This creates a new UITabBarController instance - but you want to access the one that was instantiated by the storyboads and that is presented on the screen. However, without more context it's hard to give you suggestions on how to access it. Considering that you call this code from a viewController directly embedded in the tab bar controller, I would start with this:
if let tabBarController = self.tabBarController {
tabBarController.selectedIndex = 1
var controllers = tabBarController.viewControllers
controllers.remove(at: 2)
tabBarController.viewControllers = controllers
}

Try this:
if let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController {
var viewControllers = tabBarController.viewControllers
viewControllers.remove(at: 2)
tabBarController.viewControllers = viewControllers
tabBarController.selectedIndex = 1
}

if let tabBarController = self.tabBarController {
let indexToRemove = 3
if indexToRemove < tabBarController.viewControllers?.count {
var viewControllers = tabBarController.viewControllers
viewControllers?.remove(at: indexToRemove)
tabBarController.viewControllers = viewControllers
}
}

Related

After creating a tab bar controller inside a view controller, I can't interact with the view controller that is displayed; why not?

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

Segue to a View Controller from my AppDelegate removes my UI Tab Bar

I am having a problem when performing segue from my AppDelegate. I am using this code to do a segue from appdelagate:
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let viewController: MyViewController = storyboard.instantiateViewController(withIdentifier: "myviewcontroller") as! MyViewController
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.show(viewController, sender: self)
When I use this, my UITabBar is removed. I want to segue to a ViewController that is not a TabBar item and retain my UITabbar. I am also using navigation. How should I approach this?
To show the tab , you need to do the push from 1 of the tab vcs , and it should be embeded inside a navigation and use
let nav = self.window!.rootViewController as! UINavigationController
if let tab = nav.viewControllers.first as? UITabBarController ,
let innerNav = tab.viewControllers.first as? UINavigationController {
innerNav.pushViewController(viewController,animated:true)
}

programmatically set initial view controller to tab controller swift 2

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

Same View controller in Tab bar controller for 4 tabs is giving wrong selected tabcontroller selected index

I am trying to make a template for my app... lets say my app loads the same view controller which has a CollectionView for 4 tabs. According to selected index, I have to load the contents into collection view. I am setting up the tab bar manually from Appdelegate. My question is Is this possible like instantiating same viewcntroller for all 4 tabs of Tabbarcontroller at a time. if yes, how will i know correctly that which index is selected?
Code for tabBarcontroller in Appdelegate
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tabBarController = UITabBarController()
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let firstImage = UIImage(named: "image1")
let secondImage = UIImage(named: "image2")
var controllers = [UIViewController]()
for var i = 0; i < self.myList.count; i++ {
let vc : ViewControllerTwo = storyboard.instantiateViewControllerWithIdentifier("view1") as! ViewControllerTwo
if(i == 0 || i == 3)
{
vc.tabBarItem = UITabBarItem(
title: self.myList[i],
image: firstImage,
tag: i)
controllers.append(vc)
}
else
{
vc.tabBarItem = UITabBarItem(
title: self.myList[i],
image: secondImage,
tag: i)
controllers.append(vc)
}
}
self.tabBarController.viewControllers = controllers
self.window?.rootViewController = self.tabBarController
self.self.window?.rootViewController = self.tabBarController
self.window?.makeKeyAndVisible()
If you set your class as the delegate for your tab bar controller, you will get a call to the didSelectViewController delegate method. You can then use your controllers array to determine the index;
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tabBarController = UITabBarController()
tabBarController.delegate = self
func tabBarController(_ tabBarController: UITabBarController,
didSelectViewController viewController: UIViewController) {
if let index = self.controllers.indexOf(viewController) {
// Do something with index
}
}

Tab bar item not showing

if(!isUserLoggedIn){
var loginViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
}else{
var mainViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("mainView") as! FirstViewController
var myDealsViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("myDealsViewController") as!MyDealsViewController
var settingsViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("settingsView") as! SettingsViewController
var centerNav = UINavigationController(rootViewController: mainViewController) as UINavigationController
var myDealNavController = UINavigationController(rootViewController: myDealsViewController) as UINavigationController
let controllers = [centerNav,myDealNavController,settingsViewController]
tabBarController.viewControllers = controllers
window!.rootViewController = tabBarController
window!.makeKeyAndVisible()
}
the above code is working but the app is not showing the tab bar item even though it works when I click on the center tab bar item.
Replace line of your code with :
var myDealNavController = mainStoryBoard.instantiateViewControllerWithIdentifier("myDealNavController") as! UINavigationController
And set "myDealNavController" as identifier to your UINavigationController in storyboard.

Resources