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
Related
I have three viewcontrollers that all have buttons switch to a fourth viewcontroller. On this fourth viewcontroller, I have a back button, which I want to take me back to the viewcontroller that I was originally at.
I can't just use the control drag since that only let's you go to a single controller. How can I programmatically have it send the user to the most recent viewcontroller?
import UIKit
class ViewController: UIViewController {
let tabBar = UITabBarController()
var selectedIndex: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tab()
}
#IBAction func goto(_ sender: Any) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "FrekansViewController") as! FrekansViewController
self.present(nextViewController, animated:true, completion:nil)
}
func tab() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC = UIViewController()
var streamVC = UIViewController()
var liveVC = UIViewController()
var searchVC = UIViewController()
streamVC = storyboard.instantiateViewController(withIdentifier: "StreamViewController")
liveVC = storyboard.instantiateViewController(withIdentifier: "LiveViewController")
searchVC = storyboard.instantiateViewController(withIdentifier: "SearchViewController")
tabBar.viewControllers = [homeVC,streamVC, liveVC, searchVC]
let itemHome = UITabBarItem(title: "Home", image: UIImage.init(systemName: "house.fill") , tag:0)
let itemStream = UITabBarItem(title: "List", image: UIImage.init(systemName: "waveform.path") , tag:1)
let itemLive = UITabBarItem(title: "Radio", image: UIImage.init(systemName: "play.fill") , tag:2)
let itemSearch = UITabBarItem(title: "Search", image: UIImage.init(systemName: "magnifyingglass"), tag: 3)
homeVC.tabBarItem = itemHome
streamVC.tabBarItem = itemStream
liveVC.tabBarItem = itemLive
searchVC.tabBarItem = itemSearch
self.view.addSubview(tabBar.view)
}
}
Try to embed the controllers inside NavigationController then you can use :
navigationController?.popViewController(animated: true)
This would do the desired functionality for you.
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.
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 trying to keep bottom tab bar in children viewcontroller , my issue when i open children window its open without bottom tabs , how i can keep bottom tabs stuck everywhere in the app ?
this is the class of main tabs window ( landing )
class vc_landingPage: UITabBarController , UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
//Delegate methods
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
print("Should select viewController: \(String(describing: viewController.title)) ?")
return true;
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let sb2 = UIStoryboard(name: "pools", bundle: nil)
let v1 = sb2.b_pools.instantiateInitialViewController()!
v1.tabBarItem = UITabBarItem( title : "" , image: UIImage(named: "icon-pools-x30"), selectedImage: UIImage(named: "icon-pools-x30-active"))
let sb = UIStoryboard(name: "myProfile", bundle: nil)
let v2 = sb.instantiateInitialViewController()!
v2.tabBarItem = UITabBarItem( title : "" , image: UIImage(named: "icon-profile-x30"), selectedImage: UIImage(named: "icon-profile-x30-active"))
self.viewControllers = [v1,v2]
self.selectedIndex = 1
}
}
Please you must manage the children window in a UINavigationController.
let v1 = sb2.b_pools.instantiateInitialViewController()!
let navi1 = UINavigationController.init(rootViewController: v1)
let v2 = sb.instantiateInitialViewController()!
let navi2 = UINavigationController.init(rootViewController: v2)
self.viewControllers = [navi1,navi2]
Make sure your BOTTOM BAR is not none, make sure it is not set to any of your view controller or your tab bar controller
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.