簡體   English   中英

iOS 8 Swift導航欄標題,按鈕在基於選項卡的應用程序中不顯示

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

我試圖按照本教程本答案本答案來為iOS 8 / Swift中基於選項卡的應用程序中的每個選項卡創建導航欄,但是導航欄上沒有顯示標題或按鈕。

這是我到目前為止的內容:

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

但是我只是在模擬器頂部獲得了一個空白的導航欄。

iPhone 6模擬器屏幕截圖

你讓一個自定義UINavigationBar ,當一個已經與提供給您UINavigationController

在您的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
}

無需使用新的導航欄,只需使用現有的導航欄即可。 記住你在這里做什么:

let nc1 = UINavigationController(rootViewController: vc1)

因此,您的視圖控制器已經嵌入了導航控制器中,只需使用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

我不必要創建兩個導航欄。 導航控制器帶有導航欄,我將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
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM