我试图改变设置按钮的颜色为白色,但不能让它改变。

这两种方法我都试过:

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

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

我怎么把那个按钮变成白色?


当前回答

Swift 5更新

如果你需要全局设置后退按钮颜色,你可以简单地使用:

UIBarButtonItem.appearance().tintColor = Asset.pureWhite.color

然后你不需要在每个视图控制器上设置后退按钮的背景色。如果你使用这个,你不能在其他视图控制器上设置返回按钮的颜色

BUT

如果您需要在视图控制器上设置后退按钮颜色或在另一个视图控制器上更改,请不要使用上述方法。你可以用:

let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.font:FontFamily.BatonTurbo.medium.font(size: 20),
                                  .foregroundColor: Asset.pureWhite.color] // Naviagtion Title attributes
appearance.backgroundColor = .red // Navigation bar background color

self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance

navigationController?.navigationBar.tintColor = .green // Back button color

其他回答

如果你试了很多次都没有成功,你可以试试:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red

其实,我试了很多次,才发现这种方法行得通。

对于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
}
self.navigationController?.navigationBar.tintColor = UIColor.redColor()

这段代码具有魔力。而不是红色,改变它作为你的愿望。

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

这适用于我,iOS 9.0+

在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()]