I want to call the UITabBarController after signing in from a UIViewController
I use the pushViewController but it doesn't work.
Here's my code
let dashboarController = DashboardTabBarController()
self.navigationController?.pushViewController(dashboarController, animated: false)
self.dismiss(animated: true, completion: nil)
This is my code in DashboardController
DashboardTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.view.backgroundColor = UIColor.white
print("test")
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let tabOne = MerchantTableViewController()
let tabOneBarItem = UITabBarItem(title: "Merchant", image: UIImage(named: "icon_merchant"), selectedImage: UIImage(named: "icon_merchant"))
tabOne.tabBarItem = tabOneBarItem
let tabTwo = RewardsViewController()
let tabTwoBarItem2 = UITabBarItem(title: "Rewards", image: UIImage(named: "icon_reward"), selectedImage: UIImage(named: "icon_reward"))
tabTwo.tabBarItem = tabTwoBarItem2
let tabThree = ViewController()
let tabTwoBarItem3 = UITabBarItem(title: "Promos", image: UIImage(named: "icon_promos"), selectedImage: UIImage(named: "icon_promos"))
tabThree.tabBarItem = tabTwoBarItem3
let tabFour = MerchantTableViewController()
let tabTwoBarItem4 = UITabBarItem(title: "Transactions", image: UIImage(named: "icon_card"), selectedImage: UIImage(named: "icon_card"))
tabFour.tabBarItem = tabTwoBarItem4
let tabFive = ProfileViewController()
let tabTwoBarItem5 = UITabBarItem(title: "Profile", image: UIImage(named: "icon_profile"), selectedImage: UIImage(named: "icon_profile"))
tabFive.tabBarItem = tabTwoBarItem5
self.viewControllers = [tabOne, tabTwo, tabThree, tabFour, tabFive]
}
}
I'm a newbie in iOS development. Thanks
Apple doc says : A navigation controller object manages the currently displayed screens using the navigation stack, which is represented by an array of view controllers. The first view controller in the array is the root view controller. The last view controller in the array is the view controller currently being displayed. You add and remove view controllers from the stack using segues or using the methods of this class.
So now , if you don't wanted to you use UINavigationController and add the UIViewController, follow below methods:
Present ViewController
Add as a Child VC
You should have the view controller from which you are navigating is in the navigation. If it is not, then create a navigation view controller's object with root having you source view controller as root view controller.
If you don't want to use navigation controller then you can use PresentViewController method to present view controller.
Related
I have 3 view controllers in a UITabBarController. In only one view controller I would like to place it in a navigation controller. What is the proper way of doing this so that only one view controller has a Navigation Controller? I would like aController to be in a navigation controller.
import UIKit
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let mController = MViewController()
mpController.tabBarItem = UITabBarItem(title: "view1", image: UIImage(named: "viewoneimage"), tag: 0)
let inputController = InputViewController()
inputController.tabBarItem = UITabBarItem(title: "Input", image: UIImage(named: "plus"), tag: 1)
let aController = ATableViewController()
aController.tabBarItem = UITabBarItem(title: "custom", image: UIImage(named: "person.fill"), tag: 2)
let navController = UINavigationController()
// aController.navigationController = navController
viewControllers = [mController, inputController, aController, navController]
// Do any additional setup after loading the view.
}
}
You must embend your UIViewController inside the Navigation Controllers and initialize your tab menu with your Navigation Controllers.
Also for each tab you will have different Navigation Controller
Your code should look like that.
import UIKit
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let mController = MViewController()
mpController.tabBarItem = UITabBarItem(title: "view1", image: UIImage(named: "viewoneimage"), tag: 0)
let inputController = InputViewController()
inputController.tabBarItem = UITabBarItem(title: "Input", image: UIImage(named: "plus"), tag: 1)
let aController = ATableViewController()
aController.tabBarItem = UITabBarItem(title: "custom", image: UIImage(named: "person.fill"), tag: 2)
let navMController = UINavigationController(rootViewController: mpController)
let navInputController = UINavigationController(rootViewController: inputController)
let navaController = UINavigationController(rootViewController: aController)
viewControllers = [navMController, navInputController, navaController]
}
}
I am working without Storyboards.
After the successful login, I'd like to add a tabBar into my viewControllers.
I created another viewController called tabBar controller with the code:
class TabBarController: UITabBarController,UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
// Create Tab one
let tabOne = Home()
let tabOneBarItem = UITabBarItem(title: "Collection", image: #imageLiteral(resourceName: "matchTabIcon"), selectedImage: #imageLiteral(resourceName: "matchTabIconSelected"))
tabOne.tabBarItem = tabOneBarItem
// Create Tab two
let tabTwo = ScoutingVC()
let tabTwoBarItem2 = UITabBarItem(title: "Scouting", image: #imageLiteral(resourceName: "scouting"), selectedImage:#imageLiteral(resourceName: "scoutingSelected"))
tabTwo.tabBarItem = tabTwoBarItem2
self.viewControllers = [tabOne, tabTwo]
}
// UITabBarControllerDelegate method
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("Selected \(viewController.title!)")
}
}
What is the correct way to add this to all of my VCs?
I tried
self.vc.addSubView(tabBarController)
and also to create a func() in the first VC (index: 0), but either the tabBar is not there, or if there, doesn't switch between viewControllers.
func showTabBarController() {
// Create Tab one
let home = Home()
let homeTabBarItem = UITabBarItem(title: "Collection", image: #imageLiteral(resourceName: "matchTabIcon"), selectedImage: #imageLiteral(resourceName: "matchTabIconSelected"))
home.tabBarItem = homeTabBarItem
let navHome = UINavigationController.init(rootViewController: home)
// Create Tab two
let scouting = ScoutingVC()
let scoutingTabBarItem = UITabBarItem(title: "Scouting", image: #imageLiteral(resourceName: "scouting"), selectedImage: #imageLiteral(resourceName: "scoutingSelected"))
scouting.tabBarItem = scoutingTabBarItem
let navScouting = UINavigationController.init(rootViewController: scouting)
//showTabBar
tabBarCnt.viewControllers = [navHome, navScouting]
self.view.addSubview(tabBarCnt.tabBar)
}
Instead of using self.vc.addSubView(tabBarController) use present(TabBarController(), animated: false, completion: nil)
I'm trying to customize the title of my More button in my UITabBarController as I'm doing the app in another language. I subclassed UITabBarController to be able to access the tabBarController property. Unfortunately, it's always nil whether I put it in viewDidLoad or viewDidAppear(_). Any thoughts on how I can edit it?
import UIKit
class ControllerVC: UITabBarController {
let uiManager = UIManager()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
if let tabBarItem = tabBarController?.moreNavigationController.tabBarItem {
let deselectedImage = tabBarItem.image
let selectedImage = tabBarItem.selectedImage
tabBarController!.moreNavigationController.tabBarItem = UITabBarItem(title: "بیشتر", image: deselectedImage, selectedImage: selectedImage)
} else {
uiManager.showActivityIndicator(self)
}
}
}
The problem is not with moreNavigationController. The problem is with tabBarController. Your class is a UITabBarController. A tab bar controller's tabBarController property is always nil.
Just do:
let tabBarItem = moreNavigationController.tabBarItem
let deselectedImage = tabBarItem.image
let selectedImage = tabBarItem.selectedImage
moreNavigationController.tabBarItem = UITabBarItem(title: "بیشتر", image: deselectedImage, selectedImage: selectedImage)
So I have a CustomTabBarController that is created programmatically (not through storyboard). Inside of the viewDidLoad(), I create a ProfileNavController with the view controller being ProfileViewController(), also programmatically:
override func viewDidLoad() {
super.viewDidLoad()
// ProfileNavController <- ProfileController
let profileController = ProfileViewController()
let profileNavController = UINavigationController(rootViewController: profileController)
let modifiedProfileNavController = UITabBarItem(title: "Profile", image: UIImage(named: ""), selectedImage: UIImage(named: ""))
profileNavController.tabBarItem = modifiedProfileNavController
// Tab-bar selector (ignore the "recentMessagesNavController", StackOverflow)
viewControllers = [recentMessagesNavController, createDummyNavControllerWithTitle("Groups", imageName: ""), createDummyNavControllerWithTitle("Submit", imageName: ""), createDummyNavControllerWithTitle("Search", imageName: ""), profileNavController]
}
I then created a Profile.storyboard and assigned the View Controller's class to ProfileViewController in the identity inspector (in the right hand column). Inside of the storyboard view controller I put a label titled "Hello World".
The problem is that whenever I run the program and click on the Profile tab, the label (along with everything else from the storyboard) does not show up. How do I get them to show?
ProfileViewController.swift for reference:
class ProfileViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.redColor()
navigationItem.title = "Profile"
}
}
You need to intialise ProfileViewController with its storyboard reference
let profileController = ProfileViewController()
change it to
let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateInitialViewController()
Note: And I tried to set the hidesBottomBarWhenPushed to false everywhere it was possible...
Here is how I initialize my UITabBarController in my AppDelegate file:
func initTabBarController()
{
let myVC1 = MapVC()
let myVC2 = MapVC()
let myVC3 = MapVC()
let myVC4 = MapVC()
let controllers = [myVC1,myVC2,myVC3,myVC4]
self.myTabBarController = UITabBarController()
self.myTabBarController.viewControllers = controllers
myVC1.tabBarItem = UITabBarItem(
title: "Map",
image: image1,
selectedImage: image11)
myVC2.tabBarItem = UITabBarItem(
title: "Map",
image: image2,
selectedImage: image21)
myVC3.tabBarItem = UITabBarItem(
title: "Map",
image: image3,
selectedImage: image31)
myVC4.tabBarItem = UITabBarItem(
title: "Menu",
image: image4,
selectedImage: image41)
self.tabNavigationController = UINavigationController(rootViewController: self.myTabBarController)
self.tabNavigationController.navigationBar.translucent = false
}
Now Here is how I set the rootViewController of my main window:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.initTabBarController()
appDelegate.window!.rootViewController =
appDelegate.tabNavigationController
appDelegate.window!.makeKeyAndVisible()
And here is finally how I try to push a new view controller inside one of my ViewController (MapVC):
let v = UIViewController()
v.view.backgroundColor = UIColor.yellowColor()
self.tabBarController?.navigationController?.pushViewController(v, animated: true)
When this code is executed, the Yellow view is well displayed, but the bottom tab bar is hidden.
And I'd like to still have my Tab Bar!!!
I tried to set the property hidesBottomBarWhenPushed to false to any object I can, unsuccessfully.
Please help me!!!
Regards,
Alx
It looks like you have embedded your tabBarController in a NavigationController. That is probably why the tabs are hidden when a new ViewController is pushed on the stack. Instead embed each of the tabBarController's ViewControllers inside their own NavigationController.