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

Bra*_*ans 174 uinavigationbar uibarbuttonitem ios backbarbuttonitem swift

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

我试过这两个:

navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

如何将该按钮设为白色?

Etg*_*gar 252

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

故事板中的全局色调选项

  • 很好的发现,但是将全局色调改为白色(根据原始问题)也会将表格视图详细信息披露指标等内容更改为白色,这会使它们在相关表格中不可见. (9认同)
  • 哇..好找!你知道如何以编程方式这样做吗? (7认同)
  • 虽然这是一个快速简便的解决方案,但如果颜色为白色/与背景匹配,则会导致其他问题.如果你有文本字段,它会使光标不明显,这使得用户看起来好像他们无法在文本字段上写,因为光标不可见. (4认同)
  • @PaulLehn要以编程方式执行此操作,请在AppDelegate.swift> application(application:didFinishLaunchingWithOptions :)中编写:self.window!.tintColor = .yourColor` (3认同)

Cha*_*van 189

此代码更改箭头颜色

self.navigationController.navigationBar.tintColor = UIColor.whiteColor();
Run Code Online (Sandbox Code Playgroud)

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

self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()
Run Code Online (Sandbox Code Playgroud)

Swift 3笔记

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

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

self.navigationController?.navigationBar =
Run Code Online (Sandbox Code Playgroud)

  • `self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()`为我工作(Swift 2.2) (7认同)
  • 它仅更改箭头,背面文本在 Xcode 11 中仍然保持不变 (2认同)

Ale*_*oDP 88

在故事板中很容易设置:

在此输入图像描述

在此输入图像描述

  • 谢谢man,故事板的真正解决方案. (6认同)

Tie*_*Van 50

你应该用这个:

navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
Run Code Online (Sandbox Code Playgroud)


Dee*_*iya 26

斯威夫特3

 override func viewDidLoad() {
     super.viewDidLoad()

 self.navigationController?.navigationBar.tintColor = UIColor.white
 }
Run Code Online (Sandbox Code Playgroud)


Moh*_*din 19

你可以像这样使用.放在里面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
    }
Run Code Online (Sandbox Code Playgroud)


AiO*_*OsN 16

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
    }
Run Code Online (Sandbox Code Playgroud)

更改特定控制器

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

present(navController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Vin*_*ent 14

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

self.navigationController?.navigationBar.tintColor = UIColor.red
Run Code Online (Sandbox Code Playgroud)


小智 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
Run Code Online (Sandbox Code Playgroud)


Vai*_*wad 9

    self.navigationController?.navigationBar.tintColor = UIColor.black // to change the all text color in navigation bar or navigation 
    self.navigationController?.navigationBar.barTintColor = UIColor.white // change the navigation background color
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] // To change only navigation bar title text color
Run Code Online (Sandbox Code Playgroud)


小智 9

如果您尝试了多次但无法成功,您可以尝试:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red
Run Code Online (Sandbox Code Playgroud)

其实我尝试了很多次,只发现这个方法可行。


Cha*_*lva 9

Swift 5 更新

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

UIBarButtonItem.appearance().tintColor = Asset.pureWhite.color
Run Code Online (Sandbox Code Playgroud)

那么您不需要在每个视图控制器上设置后退按钮背景颜色。如果您使用此控制器,则无法在另一个视图控制器上设置后退按钮颜色

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

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
Run Code Online (Sandbox Code Playgroud)


Sur*_*raj 6

self.navigationController?.navigationBar.tintColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)

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


小智 6

斯威夫特3

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

在此输入图像描述

更改颜色的正确代码是:

self.navigationController?.navigationBar.tintColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)

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


小智 6

AppDelegate课堂上使用此代码didFinishLaunchingWithOptions.

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

UINavigationBar.appearance().tintColor = .white

}
Run Code Online (Sandbox Code Playgroud)


Jam*_*iel 5

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

针对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
Run Code Online (Sandbox Code Playgroud)

请注意,这UIBarButtonItem不是的子类,UIView而是的子类NSObject。因此,它的tintColor属性不是从继承tintColorUIView

不幸的是,UIBarButtonItem.tintColor没有注释UI_APPEARANCE_SELECTOR-但这在我看来是一个文档错误。Apple Engineering在此雷达中的响应表明它受到支持。