更改导航栏中“后退”按钮的颜色

问题描述 投票:155回答:24

我正在尝试将“设置”按钮的颜色更改为白色,但无法将其更改。

我试过这两个:

navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()

但没有变化,它仍然看起来像这样:

如何将该按钮设为白色?

ios swift uinavigationbar uibarbuttonitem backbarbuttonitem
24个回答
230
投票

您可以通过单击电路板上的空白区域并在右侧工具栏中选择“显示文件检查器”来更改故事板中的全局色调颜色,您将在工具栏的底部看到“全局色调”选项。

Global Tint option in storyboard


6
投票

斯威夫特3

对于Swift 3,最受欢迎的答案是不正确的。

enter image description here

更改颜色的正确代码是:

self.navigationController?.navigationBar.tintColor = UIColor.white

如果要更改颜色,请将上面的UIColor.white更改为所需的颜色


6
投票

AppDelegate中的didFinishLaunchingWithOptions类中使用此代码。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UINavigationBar.appearance().tintColor = .white

}

5
投票
self.navigationController?.navigationBar.tintColor = UIColor.redColor()

这个片段带来了魔力。而不是redColor,将其更改为您的愿望。


4
投票

让我们试试这段代码:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor = UIColor.whiteColor()  // Back buttons and such
    navigationBarAppearace.barTintColor = UIColor.purpleColor()  // Bar's background color
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]  // Title's text color

    self.window?.backgroundColor = UIColor.whiteColor()
    return true
}

4
投票

在swift 2.0中使用

self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();

4
投票

如果您已经在“设置”视图控制器中有后退按钮,并且您想要将“付款信息”视图控制器上的后退按钮颜色更改为其他内容,则可以在“设置”视图中执行此操作控制器准备这样的segue :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "YourPaymentInformationSegue"
    {
        //Make the back button for "Payment Information" gray:
        self.navigationItem.backBarButtonItem?.tintColor = UIColor.gray               
    }
}

4
投票

设置UINavigationBar.appearance().tintColor的所有答案都与UIAppearance.h中的Apple文档冲突。

iOS7的注意事项:在iOS7上,tintColor属性已移至UIView,现在具有UIView.h中描述的特殊继承行为。此继承行为可能与外观代理冲突,因此现在不允许tintColor使用外观代理。

在Xcode中,您需要按命令单击要与外观代理一起使用的每个属性,以检查头文件并确保使用UI_APPEARANCE_SELECTOR注释该属性。

因此,通过外观代理为整个应用程序着色导航栏紫色以及标题和按钮白色的正确方法是:

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().tintColor = .white

请注意,UIBarButtonItem不是UIView的子类,而是NSObject。所以它的tintColor财产不是来自tintColor的继承的UIView

不幸的是,UIBarButtonItem.tintColor没有使用UI_APPEARANCE_SELECTOR注释 - 但在我看来这是一个文档错误。 Apple Engineering在this radar的回应表明它得到了支持。


3
投票

将以下代码添加到AppDelegate.swift中的didFinishLaunchingWithOptions函数

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) // White color
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517) // Green shade

// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]

2
投票

对于Swift 2.0,要在AppDelegate.swift中使用以下内容更改导航栏色调颜色,标题文本和后退按钮色调颜色

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

  // Override point for customization after application launch.


    //Navigation bar tint color change

    UINavigationBar.appearance().barTintColor = UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 0.5)

    //Back button tint color change

    UINavigationBar.appearance().barStyle = UIBarStyle.Default
    UINavigationBar.appearance().tintColor =  UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1)

    //Navigation Menu font tint color change

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1), NSFontAttributeName: UIFont(name: "OpenSans-Bold", size: 25)!]//UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 1.0)

    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent


    return true
}

2
投票
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

这适用于我,iOS 9.0+


167
投票

此代码更改箭头颜色

self.navigationController.navigationBar.tintColor = UIColor.whiteColor();

如果这不起作用,请使用以下代码:

self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()

Swift 3笔记

UIColor.whiteColor()和类似的已被简化为UIColor.white

此外,许多以前隐式的选项已更改为显式,因此您可能需要:

self.navigationController?.navigationBar =

0
投票

你有一个选择隐藏你的后退按钮,并与你自己。然后设置它的颜色。

我这样做了:

self.navigationItem.setHidesBackButton(true, animated: true)
let backbtn = UIBarButtonItem(title: "Back", style:UIBarButtonItemStyle.Plain, target: self, action: "backTapped:")
self.navigationItem.leftBarButtonItem = backbtn
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.grayColor()

0
投票

不知道为什么没有人提到这个...但我正在做你在我的viewDidLoad做的正确...而且它不起作用。然后我将我的代码放入viewWillAppear,这一切都奏效了。

上面的解决方案是更改单个barbuttonItem。如果要更改代码中每个navigationBar的颜色,请按照this answer进行操作。

使用appearance()基本上改变类本身就像在应用程序中对该视图的所有实例进行全局更改。有关更多信息,请参阅here


0
投票

它将通过 - (void)viewDidLoad中的这一行解决:

self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;

0
投票

你应该添加这一行

 self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black

0
投票

我更喜欢自定义NavigationController而不是设置全局ui,或者放入ViewController。

这是我的解决方案


class AppNavigationController : UINavigationController {

  override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
  }

  override func viewWillAppear(_ animated: Bool) {

  }

}
extension AppNavigationController : UINavigationControllerDelegate {

  func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    let backButtonItem = UIBarButtonItem(
      title: "   ",
      style: UIBarButtonItem.Style.plain,
      target: nil,
      action: nil)
    backButtonItem.tintColor = UIColor.gray
    viewController.navigationItem.backBarButtonItem = backButtonItem
  }

  func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {

  }

}

如果你使用像UIBarButtonItem.appearance().tintColor = .white这样的全局设置,你也不需要像使用EKEventEditViewController,PickerViewController等Apple Api一样混乱


85
投票

在故事板中很容易设置:

enter image description here

enter image description here


44
投票

你应该用这个:

navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white

18
投票

你可以像这样使用。把它放在AppDelegate.swift里面。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().translucent = false
        UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

        return true
    }

18
投票

迅速

 override func viewDidLoad() {
     super.viewDidLoad()

 self.navigationController?.navigationBar.tintColor = UIColor.white
 }

14
投票

在Swift3中,将“后退”按钮设置为red

self.navigationController?.navigationBar.tintColor = UIColor.red

13
投票

在Swift 4中,您可以使用以下方法处理此问题:

let navStyles = UINavigationBar.appearance()
// This will set the color of the text for the back buttons.
navStyles.tintColor = .white
// This will set the background color for navBar
navStyles.barTintColor = .black

12
投票

Swift 4.2

更改完整的应用主题

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().tintColor = .white

        return true
    }

更改特定控制器

let navController = UINavigationController.init(rootViewController: yourViewController) 
navController.navigationBar.tintColor = .red

present(navController, animated: true, completion: nil)
© www.soinside.com 2019 - 2024. All rights reserved.