ios 更改导航栏中返回按钮的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28733936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:00:05  来源:igfitidea点击:

Change color of Back button in navigation bar

iosswiftuinavigationbaruibarbuttonitembackbarbuttonitem

提问by Brandon Evans

I am trying to change the color of the Settings button to white, but can't get it to change.

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

I've tried both of these:

我已经尝试了这两种:

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

but no change, it still looks like this:

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

enter image description here

在此处输入图片说明

How do I make that button white?

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

回答by Etgar

You can change the global tint color in your storyboard by clicking on an empty space on the board and select in the right toolbar "Show the file inspector", and you will see in the bottom of the toolbar the "Global Tint" option.

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

Global Tint option in storyboard

故事板中的全局色调选项

回答by Chamath Jeevan

This code changes the arrow color

此代码更改箭头颜色

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

If this does not work, use the code below:

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

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

Swift 3 Notes

斯威夫特 3 笔记

UIColor.whiteColor()and similar have been simplified to UIColor.white

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

Also, many previously implicit optionals have been changed to explicit, so you might need:

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

self.navigationController?.navigationBar =

回答by AlessandroDP

Very easy to set up in the storyboard:

在故事板中很容易设置:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Tiep Vu Van

You should use this:

你应该使用这个:

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

回答by Deepak Tagadiya

Swift

迅速

 override func viewDidLoad() {
     super.viewDidLoad()

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

回答by Mohammad Nurdin

You can use like this one. Place it inside AppDelegate.swift.

你可以像这样使用。把它放在里面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
    }

回答by AiOsN

Swift 4.2

斯威夫特 4.2

Change complete app theme

更改完整的应用主题

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

        UINavigationBar.appearance().tintColor = .white

        return true
    }

Change specific controller

更改特定控制器

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

present(navController, animated: true, completion: nil)

回答by Vincent

In Swift3, To set the Back button to red.

Swift3 中,要将后退按钮设置为red.

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

回答by Darrell Richards

In Swift 4, you can take care of this issue using:

在 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

回答by Elon R.

Swift 3

斯威夫特 3

The most upvoted answer is not correct for Swift 3.

对于 Swift 3,最受好评的答案是不正确的。

enter image description here

在此处输入图片说明

The correct code to change color is:

更改颜色的正确代码是:

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

If you want to change color, change UIColor.white above to the desired color

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