Create Tab bar controller and Navigation controller

我的梦境 提交于 2019-12-12 04:32:07

问题


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:

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


回答1:


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".

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.

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.




回答2:


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()


来源:https://stackoverflow.com/questions/41761199/create-tab-bar-controller-and-navigation-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!