Programmatically add tab to tab bar - ios

I have an app with a UITabBarController as my initial view controller.
Currently I'm doing everything in Storyboard but I want to programmatically add a tab to the tab bar based on a user being logged in or not.
I made a TestViewController to test this out. Right now I have two tabs (pictured below). I want to have a third tab positioned on the right programmatically. I put this code in my AppDelegate's didFinishLaunching method. Based on print statements the view controller is being added to the tab bar but it is not appearing in the tab bar then the app loads.
Any suggestions?
func addTabTEST() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabController = storyboard.instantiateViewControllerWithIdentifier("RootTabController") as! UITabBarController
let TestVC = storyboard.instantiateViewControllerWithIdentifier("TestViewController") as! TestViewController
let icon = UITabBarItem(title: "test", image: nil, selectedImage: nil)
TestVC.tabBarItem = icon
print("TAB CONTROLLERS 1: \(tabController.viewControllers)")
tabController.addChildViewController(TestVC)
tabController.viewControllers![2] = TestVC
print("TAB CONTROLLERS 2: \(tabController.viewControllers)")
}

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let nav1 = UINavigationController()
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let first: ViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
nav1.viewControllers = [first]
nav1.setNavigationBarHidden(true, animated: true)
nav1.title = "first"
let nav2 = UINavigationController()
let second: SecondViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
nav2.viewControllers = [second]
nav2.setNavigationBarHidden(true, animated: true)
nav2.title = "second"
let nav3 = UINavigationController()
let third: ThirdViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ThirdViewController") as! ThirdViewController
nav3.viewControllers = [third]
nav3.setNavigationBarHidden(true, animated: true)
nav3.title = "third"
let tabController = UITabBarController()
tabController.viewControllers = [nav1,nav2,nav3]
tabController.selectedIndex = 0
self.window!.rootViewController = tabController
self.window?.makeKeyAndVisible()

this is for swift 4
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController()
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let first = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
nav1.viewControllers = [first]
nav1.setNavigationBarHidden(true, animated: true)
nav1.title = "first"
let nav2 = UINavigationController()
let second: HomeViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController2") as! HomeViewController2
nav2.viewControllers = [second]
nav2.setNavigationBarHidden(true, animated: true)
nav2.title = "second"
let nav3 = UINavigationController()
let third = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController3") as! HomeViewController3
nav3.viewControllers = [third]
nav3.setNavigationBarHidden(true, animated: true)
nav3.title = "third"
let tabController = UITabBarController()
tabController.viewControllers = [nav1,nav2,nav3]
tabController.selectedIndex = 0
self.window!.rootViewController = tabController
self.window?.makeKeyAndVisible()

if you do not want to use UIStoryboard and if you have three view controllers named oneVC, twoVC, and threeVC you can use (works on Swift 5.3 with iOS 14.2)
let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = .systemBackground
self.window = window
// Put image path if you want to have an image on your TabBar for this view controller
self.oneVC?.tabBarItem = UITabBarItem(title: "One", image: nil, selectedImage: nil)
self.twoVC?.tabBarItem = UITabBarItem(title: "Two", image: nil, selectedImage: nil)
self.threeVC?.tabBarItem = UITabBarItem(title: "Three", image: nil, selectedImage: nil)
let tabController = UITabBarController()
tabController.viewControllers = [oneVC, twoVC, threeVC]
tabController.selectedIndex = 0
self.window!.rootViewController = tabController
self.window?.makeKeyAndVisible()

Related

Open ViewController on button

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.

sending ui data in swift

I'm a bit beginner in SWIFT and right now I'm facing a problem whit UI. In this PHOTO I'm showing my UI to clarify what I'm saying . in part 1 I check if the user is logged in to his account or not, if yes it goes to part 3, if not it goes to part 2. when user login in part 2, I transfer the user to part 3.
part 1 and 2 should not have any navigation color, though the part 3 should have the navigation color.
Part 1:
if let token = UserDefaults.standard.string(forKey: ConstantsKey.token){
if !token.isEmpty{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
}else{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginVc")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
}
}else{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginVc")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
}
Part 2:
let storyboard : UIStoryboard = UIStoryboard(name: "MainTabBar", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
self.present(rootController, animated: true, completion: nil)
I want to have that red color in part 3 ! but whenever I run the application in shows the defualt color of the navigation controller
does anybody knows how should I manage/handle this problem?
Then in Part 1:
if !token.isEmpty{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
rootController?.navigationBar.barTintColor = UIColor.red
self.present(rootController, animated: true, completion: nil)
Part 2:
let storyboard : UIStoryboard = UIStoryboard(name: "MainTabBar", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarVC")
let rootController = UINavigationController(rootViewController: vc)
rootController?.navigationBar.barTintColor = UIColor.red
self.present(rootController, animated: true, completion: nil)
While logging in set
UserDefaults.standard.set(true,forKey: ConstantsKey.token)
And in routing page check this
if UserDefault.standar.bool(forKey: ConstantsKey.token){
//Go to home page
}else {
setUpNav()
//Go to login page
}
setting navigation
this one will set for whole app
func setUpNav(){
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = UIColor.red
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont(name:"Cairo-Bold", size:22.0)!]
}
if you want for single VC,
write this inside that VC,
make sure that VC's root is navigationController
self.navigationController.navigationBar.barTintColor = .white
self.navigationController.navigationBar.tintColor = .red
Even if you haven't set any value, it doesn't crash !!

Add navigationController to specif viewController programmatically without storyboard swift

I want to add navigationController to just only for HomeViewController for example. I know how to do it from AppDelegate and like this below
let navBar = UINavigationController(rootViewController: homeViewController())
self.present(navBar, animated: true, completion: nil)
Is there another way that I can add navigationController inside viewDidLoad and viewWillAppear?
Edited:
My logic is when I pressed Login button which is the code below. Then it will present SWRevealViewController
#IBAction func loginPressed(_ sender: Any) {
let frontViewController = HomeViewController()
let rearViewController = TableViewController()
let swRevealVC = SWRevealViewController(rearViewController: rearViewController, frontViewController: frontViewController)
swRevealVC?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
swRevealVC?.toggleAnimationDuration = 0.30
self.present(swRevealVC!, animated: true, completion: nil)
}
I just only want to set navigationController to HomeViewController
Replace
let frontViewController = HomeViewController()
with
let frontViewController = UINavigationController(rootViewController: HomeViewController())
and it will work.
Look below code hope it works for you...
This will be in App delegate
if UserDefaults.standard.bool(forKey: REMEMBER_ME) {
let menuVC = UINavigationController(rootViewController: SideMenuViewController())
let loginVC = UINavigationController(rootViewController: DashboardViewController())
let revealViewController = SWRevealViewController(rearViewController: menuVC, frontViewController: loginVC)
self.navigationController?.navigationBar.isHidden = true
window?.rootViewController = revealViewController
} else {
window?.rootViewController = LoginViewController()
}
This will be in your login action
if let window = UIApplication.shared.keyWindow {
let menuVC = UINavigationController(rootViewController: SideMenuViewController())
let loginVC = UINavigationController(rootViewController: DashboardViewController())
let revealViewController = SWRevealViewController(rearViewController: menuVC, frontViewController: loginVC)
self.navigationController?.navigationBar.isHidden = true
window.rootViewController = revealViewController
}

Unable to display TabBar in View Controller iOS Swift

When a push notification is received, I am trying to navigate to a table view controller (ReportTVC). The Hierarchy of the view controllers in my storyboard is as shown below.
TabBarController -> Navigation Controller (Storyboard ID: CasesNavController) -> TableViewController (CasesTVC) -> TableViewController (CaseSummaryTVC) -> TableViewController(ReportTVC)
The ReportTVC is being displayed with the navigation controller as expected, but when I navigate back to the CasesTVC, I should have a TabBar with the tabs, but this is missing.
Can someone please advise how I could resolve this ?
In AppDelegate.swift:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navController = mainStoryboard.instantiateViewController as! UINavigationController
let reportTVC = mainStoryboard.instantiateViewController(withIdentifier: "ReportTVC") as! ReportTVC
reportTVC.obtainDoctorReport = true
reportTVC.caseId = caseId
navController.pushViewController(reportTVC, animated: true)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
The problem here is that you're setting the NavigationController to the as rootViewController you need to set the TabBarController as the root.
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navController = mainStoryboard.instantiateViewController(withIdentifier: "Nav") as! UINavigationController
let tabController = mainStoryboard.instantiateViewController(withIdentifier: "Tab") as! UITabBarController
let reportTVC = mainStoryboard.instantiateViewController(withIdentifier: "ReportTVC") as! ReportTVC
reportTVC.obtainDoctorReport = true
reportTVC.caseId = caseId
navController.pushViewController(reportTVC, animated: true)
tabController.setViewControllers([navController], animated: false)
self.window?.rootViewController = tabController
self.window?.makeKeyAndVisible()
You shouldn't forget that you also need the add the other ViewControllers in the TabBarViewController.

Swift : How to add icon image and image background to the UItabBar in App delegate?

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav1 = UINavigationController()
var first = ViewController(nibName: nil, bundle: nil)
nav1.viewControllers = [first]
var second = ccDiscovery(nibName: nil, bundle: nil)
var nav2 = UINavigationController()
nav2.viewControllers = [second]
var third = ccPosting(nibName: nil, bundle: nil)
var nav3 = UINavigationController()
nav3.viewControllers = [third]
var fourth = ccProfile(nibName: nil, bundle: nil)
var nav4 = UINavigationController()
nav4.viewControllers = [fourth]
var tabs = UITabBarController()
tabs.viewControllers = [nav1, nav2,nav3,nav4]
self.window!.rootViewController = tabs;
self.window?.makeKeyAndVisible();
How to add icon image and image background to the UItabBar in App delegate? I couldn't find any.
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)
UITabBarController items are set on the individual view controllers tabBarItem property:
var tabs = UITabBarController()
nav1.tabBarItem.image = UIImage(named: "some-image-1")
nav2.tabBarItem.image = UIImage(named: "some-image-2")
nav3.tabBarItem.image = UIImage(named: "some-image-3")
nav4.tabBarItem.image = UIImage(named: "some-image-4")
tabs.viewControllers = [nav1, nav2, nav3, nav4]

Resources