2016-01-10 86 views
0

在我的主導航控制器上,有一個名爲"Next Page"的右欄按鈕。按下該按鈕時,它會調用"nextPage()"這種方法,初始化導航控制器並顯示它。不在導航欄上製作欄按鈕項目和標題(iOS,Swift)

代碼爲nextPage()就像是以下幾點:

func nextPage() { 
     let window = UIWindow(frame: UIScreen.mainScreen().bounds) 
     let CustomLoginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("viewControllerID") as! customVC 

     let navVC = UINavigationController.init(rootViewController: CustomLoginVC) 
     navVC.view.autoresizingMask = [.FlexibleHeight, .FlexibleWidth, .FlexibleTopMargin, .FlexibleLeftMargin] 

     navVC.navigationBar.translucent = false 
     navVC.navigationItem.title = "haha" 
     navVC.navigationBar.tintColor = UIColor.whiteColor() 
     navVC.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "AvenirNext-Medium", size: 22)!, NSForegroundColorAttributeName: UIColor.whiteColor() ] 
     navVC.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "AvenirNext", size: 18)!], forState: UIControlState.Normal) 

     let backButton = UIBarButtonItem.init(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backButtonPressedInPDF") 
     let exportButton = UIBarButtonItem.init(image: UIImage(named: "export"), style: UIBarButtonItemStyle.Plain, target: self, action: "saveAsPDF") 

     navVC.navigationItem.setLeftBarButtonItem(backButton, animated: false) 
     navVC.navigationItem.setRightBarButtonItem(exportButton, animated: false) 
     window.rootViewController = navVC 
     self.presentViewController(navVC, animated: true, completion: nil) 
    } 

正如你看到的,我實例CustomLoginVC這是從主要故事板的類型customVCcustomVC是UIViewController的一個子類。然後,用CustomLoginVC初始化UINavigationController並分配左右欄按鈕項。但是,標題和欄按鈕項目不會顯示在導航欄上,如下圖所示。

enter image description here

我不理解爲什麼欄按鈕項目和標題沒有在導航欄上進行。

而且在AppDelegate,我設置方法類似以下內容:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     UINavigationBar.appearance().barTintColor = UIColor(hex: "00BFA5") 
     application.statusBarStyle = UIStatusBarStyle.LightContent 

     return true 
    } 

任何意見表示讚賞!

回答

1

您需要設置標題和CustomLoginVC上的按鈕,而不是navVC

+0

當我按照你的說法行事時,它就起作用了。但我不明白爲什麼我需要在CustomLoginVC上設置標題和按鈕,但不在navVC上。我不需要在UINavigationController上設置按鈕和標題嗎? CustomLoginVC不是UINavigationController,而是UIViewController – Kahsn

+1

我們總是在導航控制器的關聯視圖控制器上設置按鈕和標題。甚至相應的barbuttons的操作方法都寫在viewcontroller中,而不是導航控制器中。導航控制器只是可以推送和彈出的視圖控制器的持有者 –

0

我想你應該寫下面三行嘗試:

navVC.navigationItem.leftBarButtonItem = nil; 
    navVC.navigationItem.rightBarButtonItem = nil; 
    navVC.navigationItem.hidesBackButton = true; 

後,嘗試寫:

navVC.navigationItem.setLeftBarButtonItem(backButton, animated: false) 
navVC.navigationItem.setRightBarButtonItem(exportButton, animated: false) 

OR

self.title = "Your Title" 

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

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

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