ChatGPT解决这个技术问题 Extra ChatGPT

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

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

这两个我都试过了:

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

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

https://i.stack.imgur.com/vqCQh.png

如何使那个按钮变白?

它已经在这里,您可以查看以下链接:stackoverflow.com/a/64445477/14437411

p
pkamb

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

https://i.stack.imgur.com/BTbJU.png


哇..很好的发现!您知道如何以编程方式执行此操作吗?
很棒的发现,但是将全局色调更改为白色(根据原始问题)也会将表格视图详细信息披露指示符更改为白色,这将使它们在相关表格中不可见。
再加上 Mike 的问题:这个控件也让你所有的文本按钮都变成了这种颜色,所以你要么必须手动将它们设置为一种颜色(即显然没有默认颜色),要么只使用不同的方法。
虽然这是一个快速简单的解决方法,但如果颜色为白色/与背景匹配,则会导致其他问题。如果您有文本字段,它会使光标不明显,这使用户看起来好像他们无法在文本字段上书写,因为光标不可见。
@PaulLehn 要以编程方式执行此操作,您可以在 AppDelegate.swift > application(application:didFinishLaunchingWithOptions:) 中编写:self.window!.tintColor = .yourColor
D
David Berry

此代码更改箭头颜色

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

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

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

斯威夫特 3 笔记

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

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

self.navigationController?.navigationBar =

self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() 为我工作(Swift 2.2)
self.navigationBar.barStyle = UIBarStyle.Black self.navigationBar.tintColor = UIColor.whiteColor() 为我工作(Swift 3)
你能解释一下为什么要使用 UIBarStyle.Black 吗?
请更新:self.navigationController?.navigationBar.tintColor =
它只改变了箭头,在 Xcode 11 中后退文本仍然保持不变
A
AlessandroDP

在情节提要中很容易设置:

https://i.stack.imgur.com/0hrE5.png

https://i.stack.imgur.com/IHeMk.png


T
Tiep Vu Van

你应该使用这个:

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

这使按钮变白,但使背景颜色更浅。你知道如何让酒吧的背景再次呈现全彩吗?
这是正确的答案。通过将 barTintColor 和标题/栏按钮设置为白色,它将使栏变成紫色。
D
Deepak Tagadiya

迅速

 override func viewDidLoad() {
     super.viewDidLoad()

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

在 swift 3 中使用。
还告诉我你可以使用哪个 swift 版本?
哪个类/文件/vc 在项目中编写此代码?
在 viewWillAppear 上调用的函数 setCloseButton() 的开头。
写入 NextViewController.swift 文件,也写入 viewDidLoad()。
A
AiOsN

斯威夫特 5.5

更改完整的应用主题

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     // Set for app
     UINavigationBar.appearance().tintColor = .white
     return true
}

更改特定控制器

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

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

它会更改所有应用程序的颜色。如果我需要为特定屏幕/特定 UINavigationController 更改它怎么办?
@VyachaslavGerchicov 以上答案适用于完整的应用程序主题,如果您想更改特定的控制器。做这样的事情:让 navController = UINavigationController.init(rootViewController: yourViewController) navController.navigationBar.tintColor = .red
如果您使用的是 iOS 13,则不再使用 didFinishLaunchingWithOptions,它已移至 SceneDelegate。
N
Nurdin

你可以像这样使用。将其放在 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
    }

J
Joe

Swift3 中,将返回按钮设置为 red

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

这将设置导航栏,而不是栏按钮项
是的,这就是问题所在。
“更改导航栏中后退按钮的颜色”
这是唯一对我有用的方法。苹果现在的实施似乎有些问题......
D
Darrell Richards

在 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

V
Vaibhav Gaikwad
    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

适用于 Swift 5
E
Elon R.

斯威夫特 3

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

https://i.stack.imgur.com/GJN88.png

改变颜色的正确代码是:

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

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


这是唯一对我有用的方法。苹果现在的实施似乎有些问题......
J
Jamie McDaniel

所有答案设置 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

请注意,UIBarButtonItem 不是 UIView 的子类,而是 NSObject。所以它的 tintColor 属性不是从 UIView 继承的 tintColor

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


UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] //也
B
BartoszKP

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

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

UINavigationBar.appearance().tintColor = .white

}

S
Suresh Kumar Durairaj
self.navigationController?.navigationBar.tintColor = UIColor.redColor()

这个片段有魔力。而不是 redColor,根据您的意愿将其更改为。


d
dbc

如果您尝试了很多次但无法正常工作,您可以尝试:

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

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


你是一个生活品味,没有其他解决方案对我有用,谢谢队友
M
Mannam Brahmam

让我们试试这段代码:

 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
}

先生非常感谢您
R
Riccardo Caroli

在 swift 2.0 中使用

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

D
Despotovic

如果您的“设置”视图控制器中已经有后退按钮,并且您想将“付款信息”视图控制器上的后退按钮颜色更改为其他颜色,您可以在“设置”视图控制器中为这样的 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               
    }
}

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

这对我有用,iOS 9.0+


C
Community

不知道为什么没有人提到这一点......但我正在做你在我的 viewDidLoad 中所做的事情......但它没有工作。然后我将代码放入 viewWillAppear 中,一切正常。

上面的解决方案是改变一个single barbuttonItem。如果您想更改代码中 每个 导航栏的颜色,请按照 this answer 操作。

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


C
Chathuranga Silva

斯威夫特 5 更新

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

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

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

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

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

J
Jayprakash Dubey

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

这段代码给了我以下错误:“使用未解析的标识符'uicolorFromHex'”谁能帮我解决这个问题?
可能是 UIColor 的扩展。你可以搜索stackoverflow如何创建扩展。
y
yazh

对于 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
}

M
Muhammad Ahmed Baig

您有一个选择隐藏您的后退按钮并自己制作。然后设置它的颜色。

我这样做了:

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

该问题专门针对左侧导航项的样式,这是最接近直接回答问题的方式。
t
tylerlantern

我更喜欢自定义 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 这样的全局设置 ui,您也不需要像 EKEventEditViewControllerPickerViewController 等那样弄乱 Apple Api


C
Chathuranga

我在 swift 5 中使用它并为我工作

navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")

E
Ertaky

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

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

(lldb) p self.navigationItem.backBarButtonItem (UIBarButtonItem *) $9 = nil (lldb)
M
Monir Khlaf

你应该添加这一行

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

M
Makwan Barzan

斯威夫特 5.3:

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")

S
ScottyBlades

出于某种原因,它不适用于我的编程方式。但是,每次都将其设置在情节提要中。

https://i.stack.imgur.com/x9854.png