簡體   English   中英

當導航欄的顏色在不同的View Controller中變化時,如何更改后退按鈕的顏色?

[英]How to change the colour of the back button when the colour of the navigation bar changes across different View Controllers?

我為不同的視圖控制器使用不同的條形顏色。 一些View Controller的條形顏色要求標題文本顏色為白色,而其他View Controller的條形顏色要求顏色為黑色。 現在,我用於帶有白色導航欄的視圖控制器的代碼(在viewWillAppear方法中調用)是:

    self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!,  NSForegroundColorAttributeName: UIColor.blackColor()]

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)

我用於較深的導航欄的代碼(在viewWillAppear方法中調用)是:

    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!,  NSForegroundColorAttributeName: UIColor.whiteColor()]

     UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: UIControlState.Normal)

現在的問題是,我的前兩個視圖控制器具有白色導航欄,而我的第三個視圖控制器具有較暗的導航欄。

從第一個視圖控制器移動到第二個視圖控制器,后退按鈕通常顯示為黑色。 從第二個視圖控制器移動到第三個視圖控制器,后退按鈕再次正常顯示,並且為白色。

但是,當我按下第三個視圖控制器上的“后退”按鈕並進入第二個視圖控制器后,“后退”按鈕保持白色並且難以辨認(我不想要)。 只有當我點擊它時它才會變成黑色。 在點擊左上角之前看不到它。 我如何使它變黑?

我試過在viewDidLoad,viewDidAppear和viewWillAppear函數中調用這些方法,但這並不能解決問題。

我需要幫助解決這種令人討厭的故障。 謝謝。

無需外觀代理即可直接設置所有內容。 外觀代理會為整個應用設置值,並且應在應用運行的早期階段對其進行配置。 最好的地方是AppDelegate類的application:didFinishLaunchingWithOptions: AppDelegate 您可以這樣做:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    configureAppearance()
    return true
}

func configureAppearance() {

    // Configure for all UIBarButtonItems

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)


    // Configure for your view controller(s) where black button has to be

    UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([YourViewControllerWithBlackColorBtn.self]).setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)
}

暫無
暫無

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

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