Skip to content

Instantly share code, notes, and snippets.

@dnicolson
Created September 26, 2021 09:16
Show Gist options
  • Save dnicolson/f2711d24a12d3ffcf7116599a0d34889 to your computer and use it in GitHub Desktop.
Save dnicolson/f2711d24a12d3ffcf7116599a0d34889 to your computer and use it in GitHub Desktop.
Programmatically create a UINavigationController and UITabBarController
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let vc = ViewController()
vc.tabBarItem.title = "Tab 1"
let vc2 = ViewController()
vc2.tabBarItem.title = "Tab 2"
let vc3 = ViewController()
vc3.tabBarItem.title = "Tab 3"
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
let add2 = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
let add3 = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
vc.navigationItem.leftBarButtonItems = [add, add2, add3]
let viewControllers = [vc, vc2, vc3].map { UINavigationController(rootViewController: $0)}
viewControllers.forEach { $0.title = $0.viewControllers[0].tabBarItem.title }
let tabBarController = UITabBarController()
tabBarController.viewControllers = viewControllers
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment