我有一個真正的困難時期試圖讓我所有的導航欄元素在我的應用我創造顯現。正如你所看到的右鍵顯示得很好,但我的標題和我的後退按鈕不顯示。如果我將以下代碼更改爲我的backButtonItem
至leftBarButton
,它將正常工作。
相信我,當我說我已經嘗試過經過幾個SO帖子時,即使當我正在創建這個時,我也在查找相關的帖子,以及我找不到任何工作。
這裏是我UINavigationController
類:
import UIKit
class NavViewController: UINavigationController, UINavigationBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account
navigationBar.barTintColor = UIColor(red: 0.0627, green: 0.4862, blue: 0.0627, alpha: 1)
navigationBar.delegate = self;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
//Create the Back Button
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
backButton.tintColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1)
navigationItem.backBarButtonItem = backButton
//Create the Right Button (Go Home) Button
let rightButton = UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Plain, target: self, action: "goHome")
rightButton.tintColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1)
navigationItem.rightBarButtonItem = rightButton
//Change the Navigation Bar Title Color
navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
你可以給我任何幫助將不勝感激。 謝謝!
編輯
所以我繼續將此添加到我的appdelegate.swift,我可以得到的返回按鈕,以顯示我想隨着我的標題(S),但不是我的右按鈕不會方式顯示。
import UIKit
@UIApplicationMain class AppDelegate : UIResponder, UIApplicationDelegate {
var window : UIWindow?
var navigationItem = UINavigationItem()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window!.backgroundColor = UIColor(red: 0.2078, green: 0.2078, blue: 0.2078, alpha: 1)
//Create the Back Button
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.backBarButtonItem = backButton
//Create the Right Button (Go Home) Button
let rightButton = UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Plain, target: self, action: "goHome")
navigationItem.rightBarButtonItem = rightButton
// Assign the navigation item to the navigation bar
UINavigationBar.appearance()?.items = [navigationItem]
//Change the Navigation Bar Color
UINavigationBar.appearance()?.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UINavigationBar.appearance()?.tintColor = UIColor.whiteColor()
UINavigationBar.appearance()?.barTintColor = UIColor(red: 0.0627, green: 0.4862, blue: 0.0627, alpha: 1)
return true
}
func goHome() {
var rootViewController = self.window!.rootViewController as UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Home") as HomeViewController
rootViewController.popToViewController(homeViewController, animated: true)
}
}
我認爲後退按鈕只顯示某些類型的賽格。 – fred02138 2015-02-06 00:30:47
我不認爲這會是。如果我刪除了與後退按鈕相關的任何內容,那麼帶有箭頭的默認藍色按鈕 – KyleMassacre 2015-02-06 11:56:22
我更新了我所做的更多。 – KyleMassacre 2015-02-07 18:02:36