How Add Tabs Programmatically in Uitabbarcontroller With Swift

How add tabs programmatically in UITabBarController with swift?

UPDATE SWIFT 5

One example of how to create an UITabBarController programmatically could be like this:

First we create the UIViewControllers that will be the content for each tab of the tab bar interface. For this example we only create one very simple.

class Item1ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.green
self.title = "item1"
print("item 1 loaded")
}
}

Now, the UITabBarController:

We create the new instances of the UIViewControllers that we want to display in the tab bar. Then we create an icon for each instance we have created and then we create an array that contains all UIViewControllers that specify the content for each tab of the tab bar interface. The order of the view controllers in the array corresponds to the display order in the tab bar.

class DashboardTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let item1 = Item1ViewController()
let icon1 = UITabBarItem(title: "Title", image: UIImage(named: "someImage.png"), selectedImage: UIImage(named: "otherImage.png"))
item1.tabBarItem = icon1
let controllers = [item1] //array of the root view controllers displayed by the tab bar interface
self.viewControllers = controllers
}

//Delegate methods
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
print("Should select viewController: \(viewController.title ?? "") ?")
return true;
}
}

Making a UITabBarController and UINavigationController Programmatically Swift 4

I think there is a typo in your snippet. In the following code, we added an intermediate vc to solve recursive problems.

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

window = UIWindow(frame: UIScreen.main.bounds)
let journalVC = JournalViewController()
let navController = UINavigationController(rootViewController: journalVC)
window?.rootViewController = navController
window?.makeKeyAndVisible()

return true
}

Here is journal vc:

class JournalViewController: UIViewController{

var tabBarCnt = UITabBarController()

override func viewDidLoad() {
super.viewDidLoad()

tabBarCnt = UITabBarController()
tabBarCnt.tabBar.barStyle = .black

let journalVC = JournalTableViewController()
journalVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)

tabBarCnt.viewControllers = [journalVC]
self.view.addSubview(tabBarCnt.view)

}

}

while the tableviewcontroller should be like this:

class JournalTableViewController: UITableViewController{


}

Programmatically switching between tabs within Swift

If your window rootViewController is UITabbarController(which is in most cases) then you can access tabbar in didFinishLaunchingWithOptions in the AppDelegate file.

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.

if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}

return true
}

This will open the tab with the index given (1) in selectedIndex.

If you do this in viewDidLoad of your firstViewController, you need to manage by flag or another way to keep track of the selected tab. The best place to do this in didFinishLaunchingWithOptions of your AppDelegate file or rootViewController custom class viewDidLoad.

Embed custom tab bar controller after loginVC programmatically

Instead of using self.vc.addSubView(tabBarController) use present(TabBarController(), animated: false, completion: nil)

iOS Swift Tabbar Controller Add Item (new tab) programmatically

In viewDidload of your first tab(viewController), you can do something like,

   self.tabBarController?.viewControllers?.append(viewController)

here viewController is your 5th tab!

Can't add viewContoller to Tab Bar programmatically in the AppDelegate

In iOS 13+ the window will be nil from didFinishLaunchingWithOptions in the app delegate. Instead move your code to willConnectTo session method in SceneDelegate:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let tabBarController = window?.rootViewController as? UITabBarController {
print("rootVC")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NavController")
vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
tabBarController.viewControllers?.append(vc)
}
guard let _ = (scene as? UIWindowScene) else { return }
}

Unable to see controller added to UITabBar programmatically swift

Your app is doing exactly what your code is telling it to do. You are creating an instance of MyViewController and adding it to the UITabBarController's array of View Controllers.

Your MyViewController class file simply defines a blank, black view.

I'm guessing you created a ViewController in your Storyboard that you want to use as MyViewController? If so, you need to instantiate that from the storyboard.

When you're editing your storyboard, assign the MyViewController class to the VC you want to use, and also give it a Storyboard ID - such as MyVC. Then, edit your viewDidLoad function to this:

public override func viewDidLoad() {
super.viewDidLoad()

// wrong way
// let tabController = MyViewController()

if let tabController = storyboard?.instantiateViewController(withIdentifier: "MyVC") as? MyViewController {

let tabBarItem = UITabBarItem(title: "Menu", image: UIImage(named: "more-options.png"), selectedImage: UIImage(named: "more-options"))
tabController.tabBarItem = tabBarItem
var array = self.viewControllers
array?.append(tabController)
self.viewControllers = array

}

}


Related Topics



Leave a reply



Submit