创建标签栏控制器和导航控制器

Posted

技术标签:

【中文标题】创建标签栏控制器和导航控制器【英文标题】:Create Tab bar controller and Navigation controller 【发布时间】:2017-01-20 10:20:27 【问题描述】:

我有一个应用程序,但使用的是 XIB 文件,所以如果我在应用程序委托中添加此代码以创建标签栏控制器

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

以及创建导航控制器的代码

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

不能,因为我将self.window?.rootViewController = navigationControllerwindow?.rootViewController = tabBarController 加在一起。我想要的是这样的:

但在代码中,我需要导航控制器来推送视图控制器。

【问题讨论】:

【参考方案1】:

didFinishLaunchingWithOptions下写下代码:-

//创建标签控制器

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

【讨论】:

仅供参考,如果上述代码不能满足您的要求,请将DummyViewControllerSearchViewController 放在单独的导航控制器中(比如tabNAVViewController1、tabNavViewController2)。然后`让控制器= [tabNAVViewController1,tabNavViewController2]`【参考方案2】:

我是否可以建议一种更简单的方法,从 Storyboard 设置标签栏控制器往往会随着应用程序大小的增长而变得非常复杂且难以维护。相反,从 appdelegate 创建它并修改 didFinishLaunchingWithOptions 方法会容易得多。在此解决方案中,我显示了两个选项卡。我演示了如何从 Storyboard 设置一个选项卡,从一个视图控制器设置另一个选项卡,您可以在其中以编程方式设置内容。我还展示了如何自定义标签栏,以及如何自定义实现标签栏控制器时出现的导航栏。

    //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

如果您想知道如何在上面的示例中从主页设置故事板,您需要做的就是转到新建->文件并选择故事板。将故事板称为“Home”,当它进入您的项目时,选择它并在文件检查器中将其名称更改为“Home.storyboard”。

现在您可以转到该情节提要并添加您想要的导航控制器作为初始视图控制器以及跟随它的所有其他视图控制器。

我鼓励为每个标签设置一个故事板。它使事物保持清洁和分离。或者您可以通过编程方式完成,而无需添加情节提要,而只需从视图控制器文件中进行设置。都是一样的。

【讨论】:

以上是关于创建标签栏控制器和导航控制器的主要内容,如果未能解决你的问题,请参考以下文章

添加导航控制器后标签栏项目消失

标签栏控制器中的导航控制器

在标签栏控制器中访问多个导航控制器

如何以编程方式创建标签栏控制器后添加导航界面(Swift)

Monotouch:使用标签栏控制器和导航控制器。 AppDelegate 中有哪一个?

想在标签栏视图控制器中添加一个简单的导航栏