iOS 8 Swift 导航栏标题,按钮未显示在基于选项卡的应用程序中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31436281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:52:32  来源:igfitidea点击:

iOS 8 Swift navigation bar title, buttons not showing in tab based application

iosiphoneswiftuinavigationcontrolleruitabbarcontroller

提问by Imran

I am trying to follow this tutorial, this answer, and this answerto create navigation bars for each of my tabs in a tab-based application in iOS 8 / Swift, but no title or buttons on my navigation bar are showing.

我正在尝试按照本教程本答案本答案为 iOS 8 / Swift 中基于选项卡的应用程序中的每个选项卡创建导航栏,但导航栏上没有显示标题或按钮。

Here is what I have so far:

这是我到目前为止所拥有的:

// AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {            
    let tabBarController = UITabBarController()

    let vc1 = ViewController()
    let vc2 = ViewController()
    let vc3 = ViewController()

    let nc1 = UINavigationController(rootViewController: vc1)
    let nc2 = UINavigationController(rootViewController: vc2)
    let nc3 = UINavigationController(rootViewController: vc3)

    let controllers = [nc1,nc2,nc3]

    tabBarController.viewControllers = controllers

    nc1.tabBarItem = UITabBarItem(title: "item1", image: nil, tag: 1)
    nc2.tabBarItem = UITabBarItem(title: "item2", image: nil, tag: 1)
    nc3.tabBarItem = UITabBarItem(title: "item3", image: nil, tag: 1)

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.rootViewController = tabBarController
    window?.makeKeyAndVisible()

    return true
}

// ViewController.swift
class ViewController: UIViewController, UINavigationBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
        navigationBar.backgroundColor = UIColor.blueColor()
        navigationBar.delegate = self;

        let navigationItem = UINavigationItem()
        navigationItem.title = "Title"

        let leftButton =  UIBarButtonItem(title: "Left Button", style:   UIBarButtonItemStyle.Plain, target: self, action: nil)
        let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)

        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton

        navigationBar.items = [navigationItem]

    }

    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.TopAttached
    }
}

But I am just getting a blank navigation bar on top in the simulator.

但我只是在模拟器的顶部有一个空白的导航栏。

iPhone 6 simulator screenshot

iPhone 6 模拟器截图

回答by mbottone

You're making a custom UINavigationBarwhen one is already provided to you with the UINavigationController.

你让一个自定义UINavigationBar,当一个已经与提供给您UINavigationController

Try this instead in your ViewController:

在您的ViewController

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Title"

    let navigationBar = navigationController!.navigationBar
    navigationBar.tintColor = UIColor.blueColor()

    let leftButton =  UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
    let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)

    navigationItem.leftBarButtonItem = leftButton
    navigationItem.rightBarButtonItem = rightButton
}

回答by Aladin

No need to use a new navigation bar just use your existent navigation bar. Remember what you did here :

无需使用新的导航栏,只需使用现有的导航栏即可。记住你在这里做了什么:

let nc1 = UINavigationController(rootViewController: vc1)

So your view controller is already embed inside a navigation controller just use selfto access to the navigation items

所以您的视图控制器已经嵌入到导航控制器中,仅用于self访问导航项

self.title = "Your Title"

var homeButton = UIBarButtonItem(title: "LeftButton", style: .Plain, target: self, action: "")

var logButton = UIBarButtonItem(title: "RigthButton", style: .Plain, target: self, action: "")

self.navigationItem.leftBarButtonItem = homeButton
self.navigationItem.rightBarButtonItem = logButton

回答by Imran

I was unnecessarily creating two navigation bars. The navigation controller comes with a navigation bar, and I changed my ViewController to:

我不必要地创建了两个导航栏。导航控制器带有导航栏,我将 ViewController 更改为:

class ViewController: UIViewController, UINavigationBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "My Title"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style:   UIBarButtonItemStyle.Plain, target: self, action: nil)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
    }

    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.TopAttached
    }

}