0

I have an application but using XIB file so if I add this code in app delegate to create tab bar controller

let tabBarController = UITabBarController()
    let tabViewController1 = DummyViewController(
        nibName: "DummyViewController",
        bundle: nil)
    let tabViewController2 = SearchViewController(
        nibName:"SearchViewController",
        bundle: nil)

    tabViewController1.tabBarItem = UITabBarItem(
        title: "Location",
        image: UIImage(named: "ic_location_blue"),
        tag: 1)
    tabViewController2.tabBarItem = UITabBarItem(
        title: "Search",
        image:UIImage(named: "ic_search_blue") ,
        tag:2)


    let controllers = [tabViewController1,tabViewController2]
    tabBarController.viewControllers = controllers
    window?.rootViewController = tabBarController

and this code to create navigation controller

let viewController = SearchViewController(nibName: nil, bundle: nil)
    let navigationController = UINavigationController(rootViewController: viewController)

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

it can't because I add self.window?.rootViewController = navigationController and window?.rootViewController = tabBarController together. What I want is something like this:

enter image description here

but in code, I require navigation controller to push view controller.

2 Answers 2

1

Under didFinishLaunchingWithOptions write following code:-

//Create tab controller

let tabBarController = UITabBarController()
    let tabViewController1 = DummyViewController(
        nibName: "DummyViewController",
        bundle: nil)
    let tabViewController2 = SearchViewController(
        nibName:"SearchViewController",
        bundle: nil)

    tabViewController1.tabBarItem = UITabBarItem(
        title: "Location",
        image: UIImage(named: "ic_location_blue"),
        tag: 1)
    tabViewController2.tabBarItem = UITabBarItem(
        title: "Search",
        image:UIImage(named: "ic_search_blue") ,
        tag:2)


    let controllers = [tabViewController1,tabViewController2]
    tabBarController.viewControllers = controllers
//Create navigation controller
 let navigationController = UINavigationController(rootViewController: tabBarController)

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navigationController//Set navigation controller as window's root view
    self.window?.makeKeyAndVisible()
Sign up to request clarification or add additional context in comments.

1 Comment

FYI, In case the above code does not works as per your requirement, then put the DummyViewController and SearchViewController in separate navigation controllers (say tabNAVViewController1,tabNavViewController2). And then ` let controllers = [tabNAVViewController1,tabNavViewController2]`
0

Might I suggest a much easier approach, setting up tab bar controllers from Storyboard tends to become a very complicated and hard thing to maintain as your app grows in size. Instead, it will be much easier to create it from appdelegate and modify the didFinishLaunchingWithOptions method. In this solution, I show two tabs. I demonstrate how to setup one tab from a Storyboard and another tab from just a view controller where you might setup things programmatically. I also show how to customize the tab bars, and how to customize the nav bar that appears when you implement the tab bar controller.

    //this will hold the root
    var rootController: UIViewController!

   @UIApplicationMain
   class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
//modify didFinishLaunchingWithOptions in your app delegate as follows
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Override point for customization after application launch.

    let tabController = UITabBarController()

    //setup a view controller from another storyboard
    let workoutsStoryboard = UIStoryboard(name: "Workouts", bundle: nil)

    //this tab will start from a storyboard of its own
    let homeVC = workoutsStoryboard.instantiateViewController(withIdentifier: "home") as! HomeViewController

    //this will setup another tab bar but from a view controller only if you    want to setup things programmatically
    let profileVC = ProfileViewController()


    //setup the tab bar elements with the icons, name and initial view controllers
    let vcData: [(UIViewController, UIImage, String)] = [
        (homeVC, UIImage(named: "home_tabbar_icon")!, "Home"),
        (profileVC, UIImage(named: "feed_tabbar_icon")!, "Profile")
    ]

    let vcs = vcData.map { (vc, image, title) -> UINavigationController in
        let nav = UINavigationController(rootViewController: vc)
        nav.tabBarItem.image = image
        nav.title = title
        return nav
    }

    //customize your tab bar
    tabController.viewControllers = vcs

    tabController.tabBar.barTintColor = UIColor(hexString: "#FAFAFA")

    tabController.tabBar.tintColor = UIColor(hexString: "#4A4A4A")

    tabController.tabBar.isTranslucent = false


    if let items = tabController.tabBar.items {
        for item in items {
            if let image = item.image {

                item.image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
            }
        }
    }

    //make your tab bar the root
    window?.rootViewController = tabController


    //tab bar comes with a nav bar. here is how to customize it
    UIApplication.shared.statusBarStyle = .lightContent

    UINavigationBar.appearance().shadowImage = UIImage()

    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

    UINavigationBar.appearance().isTranslucent = false

    UINavigationBar.appearance().tintColor = UIColor.white

    UINavigationBar.appearance().backgroundColor = UIColor.white

    UINavigationBar.appearance().barTintColor = UIColor(hexString: "#4A90E2")



    rootController = (window?.rootViewController)!

    return true
}

If you are wondering how to setup the storyboard from Home in the example above all you need to do is go to New->File and select storyboard. Call the storyboard "Home" and when it comes into your project, select it and in File Inspector chane its name to "Home.storyboard".

Home storyboard

Now you can go to that storyboard an add the Navigation Controller you wanted as the initial view controller and all the other view controllers that follow it.

Home Nav Controller

I'd encourage setting up a storyboard for each tab. It keeps things clean and separated. Or you could do it programmatically without adding a storyboard and just setting up things from a view controller file. It's all the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.