Tab bar hidden by view controller when pushed inside my UITabBarController - ios

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.

Related

Black Screen Tab Bar Controller

I am facing a problem. When I want to change the View for a Tab View Controller, my application gives a black screen.
And here is my code to change view:
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
Constants.swift:
import Foundation
struct Constants {
struct Storyboard {
static let homeViewController = "homeVC"
}
}
homeVC is the first View of TabBarController, with the Label.
This is my way. I am not using storyboard by the way. I am using xib.
let dashboardVc = DashboardTabController()
self.window!.rootViewController = dashboardVc
self.window!.makeKeyAndVisible()
self.window?.overrideUserInterfaceStyle = .light
and then in dashboardTabController type this one.
import UIKit
class DashboardTabController: UITabBarController {
#IBOutlet weak var dashboardTabBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
let firstViewController = HomeViewController()
firstViewController.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "ic_home_24px"), selectedImage: UIImage(named: "ic_home_24px"))
let secondViewController = AddContactViewController()
secondViewController.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "ic_group_add_24px"), selectedImage: UIImage(named: "ic_group_add_24px"))
let tabBarList = [firstViewController, secondViewController]
viewControllers = tabBarList
}
}
don't forget in DashBoardTabController xib, implement the UITabBar in bottom. Hopefully it's helpful. Just mention me if you need more help on executing this.

How to embed a view in a navigation controller programmatically?

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]
}
}

UITabbarItem seems to ignore Swedish characters

I have a simple view controller subclassed fromUITabBarController. On assigning the items to the tab bar controller i am facing some issues with the titles of the UITabBarItem
class LandingVC: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard(name: "Ssettings", bundle: nil)
let vc1 = storyboard.instantiateViewController(withIdentifier: "bookID")
let tab1 = UITabBarItem(title: "Föo", image: nil, selectedImage: nil)
vc1.tabBarItem = tab1
let vc2 = storyboard.instantiateViewController(withIdentifier: "SettingsID")
let tab2 = UITabBarItem(title: "Sök", image: nil, selectedImage: nil)
vc2.tabBarItem = tab2
self.viewControllers = [vc1, vc2]
}
}
But strangely the tabbar controller displays items with title Sok and Foo ignoring the swedish characters. Even a tab bar systemItem seems to fail, any help would be appreciated

UITabBarItem not active on app start

I have UITabBar controller
let tabBarController = UITabBarController()
tabBarController.viewControllers = [todayViewController, forecastViewController]
window.rootViewController = tabBarController
window.makeKeyAndVisible()
tabBarController.selectedIndex = 0
inside todayViewController:
tabBarItem = UITabBarItem(title: "Today", image: #imageLiteral(resourceName: "TodayTabBarItem"), selectedImage: #imageLiteral(resourceName: "SunTabBarItemSelected"))
inside forecastViewController:
tabBarItem = UITabBarItem(title: "Forecast", image: #imageLiteral(resourceName: "ForecastTabBarItem"), selectedImage: #imageLiteral(resourceName: "ForecastTabBarItemSelected"))
When I start the app UITabBarItem of the first view controller is inactive. When I tap on it or on another one it becomes active (highlighted text and right image)
What should I do to make UITabBarItem active on app start?
Try tabBarItem = UITabBarItem not during the call to viewDidLoad, but when creating ViewContrller - in init

How navigate from one view to another without using NavigationController?

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.

Resources