Ios Swift:在自定义标签栏中显示导航栏

Posted

技术标签:

【中文标题】Ios Swift:在自定义标签栏中显示导航栏【英文标题】:Ios Swift : Displaying Navigation Bar in custom Tabbar 【发布时间】:2016-05-10 14:24:40 【问题描述】:

我正在尝试使用以下代码以编程方式创建一个自定义标签栏以将其放置在屏幕顶部。但问题是,我无法让标签栏顶部的导航栏返回上一个屏幕。 请帮助我。

override func viewDidLoad()

    super.viewDidLoad()

    let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
    let TabBarCtrVar = UITabBarController()
    let TabBarVar = TabBarCtrVar.tabBar
    let ObxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("ObxSrnUid")
    let ObxNavSrnVar = UINavigationController(rootViewController: ObxSrnVar)
    let IbxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("IbxSrnUid")
    let IbxNavSrnVar = UINavigationController(rootViewController: IbxSrnVar)

    UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar

    ObxSrnVar.tabBarItem = UITabBarItem(
        title: "Outbox",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
    IbxSrnVar.tabBarItem = UITabBarItem(
        title: "Inbox",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))

    TabBarCtrVar.viewControllers = [ObxSrnVar,IbxSrnVar]

    TabBarVar.tintColor = UIColor.greenColor()
    TabBarVar.frame = CGRectMake(
        0,
        UIApplication.sharedApplication().statusBarFrame.size.height + 64,
        UIScreen.mainScreen().bounds.width,
        50)

【问题讨论】:

您是否尝试以编程方式创建 TabBar 控制器,然后向其添加导航栏(也以编程方式)? 我需要所有 Childview 控制器(收件箱和发件箱)从父自定义标签栏 (abv) 视图控制器继承导航栏。 【参考方案1】:

这对我有用。希望对你也有用

 let myStoryBoard = UIStoryboard(name: "Main", bundle: nil)

    let myTabBarController = UITabBarController()

    let leftController = myStoryBoard.instantiateViewControllerWithIdentifier("left")

    let leftNavController = UINavigationController(rootViewController: leftController)

    let rightController = myStoryBoard.instantiateViewControllerWithIdentifier("right")

    let rightNavController = UINavigationController(rootViewController: rightController)

    UIApplication.sharedApplication().delegate!.window?!.rootViewController = myTabBarController

    myTabBarController.addChildViewController(leftNavController)
    myTabBarController.addChildViewController(rightNavController)



    let leftChild = myStoryBoard.instantiateViewControllerWithIdentifier("leftChild")
    let rightChild = myStoryBoard.instantiateViewControllerWithIdentifier("rightChild")


    leftNavController.addChildViewController(leftChild)
    rightNavController.addChildViewController(rightChild)

    print(myTabBarController.viewControllers?.count)
   let but1 = myTabBarController.viewControllers?[0].tabBarItem
    but1!.title = "Left Page"
    let but2 = myTabBarController.viewControllers?[1].tabBarItem
        but2!.title = "Right Page"


    leftNavController.childViewControllers[0].navigationItem.title = "Left View"
    rightNavController.childViewControllers[0].navigationItem.title = "Right View"

【讨论】:

【参考方案2】:

下面的代码对我有用,可以获取导航栏。但我无法添加后退按钮并使其可以移动到上一个屏幕。 谁能帮我解决这个问题。

    let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
    let TabBarCtrVar = UITabBarController()
    let TabBarVar = TabBarCtrVar.tabBar
    UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar

    let Tab1SrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("Tab1SrnXid")
    let Tab2SrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("Tab2SrnXid")

    let Tab1NavCtrVar = UINavigationController(rootViewController: Tab1SrnVar)
    let Tab2NavCtrVar = UINavigationController(rootViewController: Tab2SrnVar)

    Tab1NavCtrVar.childViewControllers[0].navigationItem.title = "Nam1TabSrn"
    Tab2NavCtrVar.childViewControllers[0].navigationItem.title = "Nam2TabSrn"

    Tab1NavCtrVar.tabBarItem = UITabBarItem(
        title: "Nam1Tab",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
    Tab2NavCtrVar.tabBarItem = UITabBarItem(
        title: "Nam2Tab",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))

    TabBarCtrVar.viewControllers = [Tab1NavCtrVar,Tab2NavCtrVar]

    TabBarVar.tintColor = UIColor.greenColor()
    TabBarVar.frame = CGRectMake(
        0,
        UIApplication.sharedApplication().statusBarFrame.size.height + 40,
        UIScreen.mainScreen().bounds.width,
        50)

【讨论】:

以上是关于Ios Swift:在自定义标签栏中显示导航栏的主要内容,如果未能解决你的问题,请参考以下文章

通过在 Swift 中添加两个标签而不是标题来自定义导航栏

在导航栏中插入图标​​/使 BarButtonItem 不可点击 Swift iOS

导航栏中的 Swift 自定义后退按钮

在导航栏中自动添加“返回”按钮

标签栏 UIViewController 中导航栏中的按钮未显示

为啥导航栏中没有显示标题?斯威夫特 3