2015-02-06 143 views
0

App Example導航欄內容不顯示

我有一個真正的困難時期試圖讓我所有的導航欄元素在我的應用我創造顯現。正如你所看到的右鍵顯示得很好,但我的標題和我的後退按鈕不顯示。如果我將以下代碼更改爲我的backButtonItemleftBarButton,它將正常工作。

相信我,當我說我已經嘗試過經過幾個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),但不是我的右按鈕不會方式顯示。

Updated Navigation Bar

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

} 
+0

我認爲後退按鈕只顯示某些類型的賽格。 – fred02138 2015-02-06 00:30:47

+0

我不認爲這會是。如果我刪除了與後退按鈕相關的任何內容,那麼帶有箭頭的默認藍色按鈕 – KyleMassacre 2015-02-06 11:56:22

+0

我更新了我所做的更多。 – KyleMassacre 2015-02-07 18:02:36

回答

1

@KyleMassacre,UINavigationController的用於導航從一個控制器到另一個控制器UINavigationController Apple Doc

它顯示自動返回按鈕。

在你AppDelegation你只需要:

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

與2個視圖控制器爲例。按鈕 「推」 從第一個視圖控制器SecondViewController:

enter image description here

在SecondViewController代碼:

import UIKit 

類SecondViewController:UIViewController的{

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    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 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 

func goHome() { 

    // navigation where you want. You can use: 
    //navigationController?.pushViewController(<#viewController: UIViewController#>, animated: <#Bool#>) 
    //navigationController?.popToRootViewControllerAnimated(animated: Bool) 
    //presentViewController(<#viewControllerToPresent: UIViewController#>, animated: <#Bool#>, completion: <#(() -> Void)?##() -> Void#>) 
} 

}

結果是:

enter image description here

+0

這是回覆。一個問題,因爲我在我的手機上,但我將不得不添加方法你有「SeconViewController」到所有其他視圖控制器?這不是什麼大不了的事情,因爲我只有5個VC,所以它不是那麼令人生畏,但我希望能夠輕鬆解決問題,如果你趕上我的漂移如果一切正常,當我嘗試它時,我會接受並給你我50分的高分 – KyleMassacre 2015-02-15 00:34:31

+0

@KyleMassacre你可以爲你的ViewControllers定義一個BaseViewController。 BaseViewController將有兩個函數可以繼承:func setRightButton(){let rightButton = UIBarButtonItem(title:「Home」,style:UIBarButtonItemStyle.Plain,target:self,action:「goHome」) rightButton.tintColor = UIColor(red :255,綠色:255,藍色:255,alpha:1) navigationItem.rightBarButtonItem = rightButton}和func goHome() – Miguel 2015-02-16 08:59:08

2

根據您的問題,似乎你不希望在一個單獨的調用設置左邊和右邊欄按鈕和u需要通過一次寫這讓他們兩人,並讓他們在所有其他類。這是你需要做的。

在AppDelegate.swift

FUNC申請(申請:UIApplication的,didFinishLaunchingWithOptions launchOptions:[NSObject的:AnyObject]?) - >布爾{

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] 
    UINavigationBar.appearance().barTintColor = UIColor(red: 0.0627, green: 0.4862, blue: 0.0627, alpha: 1) 
    UINavigationBar.appearance().tintColor = UIColor.whiteColor() 
    return true 
} 

上述代碼將在整個應用程序中爲導航欄設置此屬性。

enter image description here

現在,添加新文件並將它命名爲RootViewController的 - 「的子類 - 的UIViewController」。

enter image description here

進口的UIKit

類RootViewController的:UIViewController的{

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func setLeftBarButton() { 
    // Below line will Create Custom leftBarButton. 
    var backBtn = UIBarButtonItem(title: "First", style: UIBarButtonItemStyle.Bordered, target: self, action: "popBack") as UIBarButtonItem 
    self.navigationItem.leftBarButtonItem = backBtn 
    // Below line will Remove default backBarButton. 
    self.navigationItem.backBarButtonItem = nil; 
} 

func setRightBarButton() { 
    let rightBtn: UIBarButtonItem = UIBarButtonItem(title: "Home", style: UIBarButtonItemStyle.Bordered, target: self, action: "goToNext") 
    self.navigationItem.rightBarButtonItem = rightBtn 
} 

func popBack() { 
    self.navigationController?.popViewControllerAnimated(true) 
} 

func goToNext() { 
    print("Go to Next View.") 
} 

現在在所有的瀏覽器,你只需要做的是「替換 - 用的UIViewController RootViewController的」。

enter image description here

現在在第二個視圖控制器,所有你需要做的是從RootViewController的調用方法SecondViewController的viewDidLoad中。
倍率FUNC viewDidLoad中(){
super.viewDidLoad()
self.navigationItem.title = 「第二個視圖」
setLeftBarButton()
setRightBarButton()
//是否有任何額外的設置加載後視圖。
}

輸出:
enter image description here

希望這可以節省您的問題。