I am not able to access tabbarcontroller from appdelegate in swift ios - ios

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window?.backgroundColor = UIColor.white
self.window?.makeKeyAndVisible()
let tabBarController = UITabBarController()
let myVC1 = ViewController(nibName: "ViewController", bundle: nil)
let myVC2 = ViewController1(nibName: "ViewController1", bundle: nil)
let controllers = [myVC1,myVC2]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
let firstImage = UIImage(named: "Search")
let secondImage = UIImage(named: "Search1")
myVC1.tabBarItem = UITabBarItem(
title: "Pie",
image: firstImage,
tag: 1)
myVC2.tabBarItem = UITabBarItem(
title: "Pizza",
image: secondImage,
tag:2)
// Override point for customization after application launch.
return true
A black screen appears whenever I run the app.

I think you did not pervoid UIWindow size
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UITabBarController()
or you some problem with setting root view controller.

You need to add window = UIWindow at the beginning of app Delegate didFinishLaunchingWithOptions: or else there isn't a frame size set. Thats why your getting the black screen.
window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.white
let tabBarController = UITabBarController()
let myVC1 = UIViewController()
let myVC2 = UIViewController()
let controllers = [myVC1,myVC2]

Related

Set UITabBarController as rootViewController

I have a UITabBarController and I want to show this screen and not the login screen if the user session is still active.
My UITabBarController has 3 ViewControllers and the problem is that I don't see the TabBar links in the bottom and I'm unable to navigate.
Without the following code everything works fine. I mean after login I can navigate.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
if Auth.auth().currentUser != nil {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = HomeTabBarController()
}
return true
}
I have also tried the following code to set the rootViewController but it's the same problem.
When I try to set one of the other view controllers as root (one of the children), the Tab Bar doesn't show at all
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
What am I doing wrong here?
I was facing same issue and I came across your post, the problem with your code is that HomeTabBarController() is creating a whole new TabBarController so to fix it try the following approach I used.
if Auth.auth().currentUser != nil {
print("******************************User Present******************************")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Interface Builder -> Identitiy Inspector -> Storyboard ID
// Set up the Tab Bar Controller to have two tabs
let tabBarController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
Edit Make sure to add the Identifier to your TabBarController
// Interface Builder -> Identitiy Inspector -> Storyboard ID
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//// this code changes the initial point of aap///////
window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController()
let myview = SettingTabbar()
nav.viewControllers = [myview]
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
//Setting TabBar
let tabbar = UITabBarController()
//Designing Tabbar Item Images
let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)
//Getting TabBar ViewControllers
let TableView = newViewController()
let CollectionView = PersonCollectionViewController()
let InsertRec = nextViewController()
//Setting ViewControllers on TabBar Items
TableView.tabBarItem = table
CollectionView.tabBarItem = collection
InsertRec.tabBarItem = insert
let controllers = [TableView,CollectionView,InsertRec]
tabbar.viewControllers = controllers
tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
//Setting Title
tabbar.navigationItem.title = "Person Record"
return tabbar
}
The problem is this line:
window?.rootViewController = HomeTabBarController()
That is the wrong HomeTabBarController. It is a totally new HomeTabBarController with no children. You need to fetch the HomeTabBarController that’s in the storyboard.
I finally found the solution: As #matt suggested I had to fetch the HomeTabBarController that’s in the storyboard.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
// controller identifier sets up in storyboard utilities
// panel (on the right), it called Storyboard ID
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
window?.makeKeyAndVisible()
window?.rootViewController = viewController

Navigation bar appear from black color?

I am setting a new navigation for my app on launch. But when I launch it appears from a black color animation.After black color it sets it navigation bar. Please tell me what is issue.
I am using below code
var controller = UIViewController()
//App Theming
var navController = UINavigationController()
navController.navigationBar.barTintColor = UIColor.white
navController.navigationBar.tintColor = UIColor.white
navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navController.navigationBar.shadowImage = UIImage()
navController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navController.navigationBar.isTranslucent = false
navController = UINavigationController(rootViewController: viewcontroller)
navController.navigationBar.isHidden = true
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = navController
appDelegate.window?.makeKeyAndVisible()
The problem is this line:
navController.navigationBar.isHidden = true
Delete it and try again.
Please use snippet bellow
In this I am using ViewController from Main storyboard
// mainStoryboard
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
// rootViewController
let rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
// navigationController
let navigationController = UINavigationController(rootViewController: rootViewController!)
//App Theming
navigationController.navigationBar.barTintColor = UIColor.white
navigationController.navigationBar.tintColor = UIColor.white
navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navigationController.title = "Testing Th"
navigationController.navigationBar.shadowImage = UIImage()
navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.isHidden = true
// self.window
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
If I unhide NavigationBar
navigationController.navigationBar.isHidden = false
you can clearly see the results
I can see that you did not give the navigationController any viewControllers.
You need to pass the navigation controller at least one viewController for it to know which viewController to start your
navigation process from, follow the code below:
var window: UIWindow?
let nav = UINavigationController()
1- here is where i declared my Initial viewController (where i want my navigation process to start from)
var main = HomeViewController(nibName: "HomeViewController", bundle: nil)
2- here is where i give the navigationController the first viewController to start from.
window?.rootViewController = nav
nav.viewControllers = [main]//you need to have this line
nav.isNavigationBarHidden = true
window?.makeKeyAndVisible()
alright i just noticed you're using storyboards, give this a try:
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var ivc = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
navigationController?.pushViewController(anIvc, animated: true)
window.rootViewController = ivc
window.rootViewController = navigationController
window.makeKeyAndVisible()
Your code should be placed in
import UIKit
// AppDelegate class file
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Be attentive. controller allocated withou any layout. Change to custom controller class or load from IB resource (storyboard/nib)
let controller = UIViewController()
let navigationController = rootNavigationController
// Setup viewControllers. Just one controller as root
navigationController.viewControllers = [controller]
// You already have a reference to window in your AppDelegate
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
extension AppDelegate {
// Move out of AppDelegate class code to create theming NavigationController
private var rootNavigationController: UINavigationController {
let navController = UINavigationController()
navController.navigationBar.barTintColor = UIColor.white
navController.navigationBar.tintColor = UIColor.white
navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navController.navigationBar.shadowImage = UIImage()
navController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navController.navigationBar.isTranslucent = false
navController.navigationBar.isHidden = true
// If it theme for all application you should use appearances
/* For Example
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor = .white
*/
return navController
}
}
You possible errors:
controller instance haven't any layout. By default ViewController
haven't anything
You code call from separated place. It's market by you call of AppDelegate instance. Configure you rootViewController in
didFinishLoading
You have an separated ViewController that placed as root

How can I embed my navigation controller into my tab bar controller programmatically in Swift Xcode 8.2?

I have been teaching my self how to develop and I got to the point to where I need guidance from experts. I created some of the user interface programmatically using the MVC structure. My question is how can I embed my navigation controller into my tab bar controller so my tab bar controller can be on every screen. I made the tab bar controller in main.storyboard and referenced it in the View controller I named Home Controller.
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabViewController = storyboard.instantiateViewController(withIdentifier: "TabBar")
self.present(tabViewController, animated: true, completion: nil)
The code above is in the view did load function. I was wondering do I need to change up the root view controller in my app delegates?
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
This is in my finished launched func.
Use following lines
var navigationController = UINavigationController(rootViewController: viewController));
tabBarController.viewControllers = [navigationController,firstViewControllersecondViewController, ]
create a separate UITabBarController class and initialize it in AppDelegate like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = MyTabBarController()
window?.makeKeyAndVisible()
return true
}
and the custom tabBarController
import UIKit
class MyTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.barTintColor = UIColor.black
self.tabBar.tintColor = UIColor.white
self.tabBar.unselectedItemTintColor = UIColor.white.withAlphaComponent(0.4)
let firstViewController = FirstViewController()
let firstViewTabBarItem = UITabBarItem(title: "First", image: UIImage(named: "calculator"), selectedImage: UIImage(named: "calculator"))
firstViewController.tabBarItem = firstViewTabBarItem
firstViewController.tabBarItem.tag = 0
let historyViewController = HistoricDataViewController()
historyViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .history, tag: 1)
let tabBarList = [calculateViewController, historyViewController]
viewControllers = tabBarList.map{UINavigationController(rootViewController: $0)}
}
}
Hope this example helps.

I am missing my tabbar after configuring tabbar controller in appdelegate in swift ios

let tabBarController = UITabBarController()
let VC1 = ConstDetailViewController()
VC1.tabBarItem.image = UIImage(named: "Home.Re.png")
let VC1Navigation = UINavigationController(rootViewController: VC1)
let VC2 = LeaderboardViewController()
VC2.tabBarItem.image = UIImage(named: "search3.png")
let VC2Navigation = UINavigationController(rootViewController: VC2)
let VC3 = MapViewViewController()
VC3.tabBarItem.image = UIImage(named: "graphRe.png")
let VC3Navigation = UINavigationController(rootViewController: VC3)
let VC4 = UserProfileViewController()
VC4.tabBarItem.image = UIImage(named: "UserRe.png")
let VC4Navigation = UINavigationController(rootViewController: VC4)
let loginVC = ConstituencyViewController()
let VC5Navigation = UINavigationController(rootViewController: loginVC)
let controllers = [VC1Navigation, VC2Navigation,VC3Navigation,VC4Navigation]
tabBarController.viewControllers = controllers
self.window!.backgroundColor = UIColor.white
self.window?.rootViewController = VC5Navigation
self.window!.makeKeyAndVisible()
I have configured tab bar in my appdelegate. But now I am missing my tab bar. It shows a blank space in the place of tab bar. Please help me resolve the issue.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let tabBarController = UITabBarController()
let VC1 = UIViewController()
VC1.tabBarItem.image = UIImage(named: "Home.Re.png")
let VC1Navigation = UINavigationController(rootViewController: VC1)
let VC2 = UIViewController()
VC2.tabBarItem.image = UIImage(named: "search3.png")
let VC2Navigation = UINavigationController(rootViewController: VC2)
let VC3 = UIViewController()
VC3.tabBarItem.image = UIImage(named: "graphRe.png")
let VC3Navigation = UINavigationController(rootViewController: VC3)
let VC4 = UIViewController()
VC4.tabBarItem.image = UIImage(named: "UserRe.png")
let VC4Navigation = UINavigationController(rootViewController: VC4)
let loginVC = UIViewController()
let VC5Navigation = UINavigationController(rootViewController: loginVC)
let controllers = [VC1Navigation, VC2Navigation,VC3Navigation,VC4Navigation]
tabBarController.viewControllers = controllers
self.window!.backgroundColor = UIColor.white
self.window?.rootViewController = tabBarController //TabBarController
self.window!.makeKeyAndVisible()
return true
}

How to use Tab Bar Controller in my AppDelegate.swift

How to use Tab Bar Controller in my AppDelegate.swift
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var centerContainer: MMDrawerController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
// Centre View Controller
let centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
// left view Controller along with centre view controller
let leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LeftSideViewController") as! LeftSideViewController
// Right view Controller along with centre view controller
let rightViewController = mainStoryboard.instantiateViewControllerWithIdentifier("RightSideViewController") as! RightSideViewController
let leftSideNav = UINavigationController(rootViewController: leftViewController)
let centerNav = UINavigationController(rootViewController: centerViewController)
let rightNav = UINavigationController(rootViewController: rightViewController)
centerContainer = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav,rightDrawerViewController:rightNav)
centerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView;
centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView;
window!.rootViewController = centerContainer
window!.makeKeyAndVisible()
return true
}
}

Resources