ChatGPT解决这个技术问题 Extra ChatGPT

iOS 8 中的 NavigationBar 栏、色调和标题文本颜色

状态栏中的背景文本仍然是黑色的。如何将颜色更改为白色?

// io8, swift, Xcode 6.0.1 
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]

}

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


A
Albert Vila Calvo

AppDelegate.swift 中,我在 application(_:didFinishLaunchingWithOptions:) 中输入了以下内容:

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]

(对于 Swift 4 或更早版本,使用 NSAttributedStringKey 而不是 NSAttributedString.Key

对于 titleTextAttributesdocs 说:

您可以在文本属性字典中为标题指定字体、文本颜色、文本阴影颜色和文本阴影偏移量


如果我们有多个导航栏怎么办?
tintColor 不适用于 Xcode 7.3.1。文字仍然是黑色的。
在 Xcode 8.2 中,titleTextAttributes 不会自动完成,但它可以工作。
barTintColor 还需要吗?删除该行仍然有效
iOS 15 中的@Albert Vila Calvo 这东西不起作用,你能告诉我该怎么做吗?
A
AG1

我喜欢亚历克斯的回答。如果您想在 ViewController 中快速尝试一些东西,请确保您使用

viewWillAppear()
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    var nav = self.navigationController?.navigationBar
    nav?.barStyle = UIBarStyle.Black
    nav?.tintColor = UIColor.white
    nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
    //nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // swift 4.2
}

https://i.stack.imgur.com/54vY0.png


但我使用的是 iso 8.1
为什么 viewWillAppear()?我在 viewDidLoad 中这样做,它工作正常。
因为每次查看 UIView 时都希望设置它。如果您在 viewDidLoad() 中设置说在 UITabBar 视图中,请移至另一个选项卡,再次设置它,然后返回它将被覆盖,因为原始视图仅加载一次。
swift 4 的更新 - NSForegroundColorAttributeName 现在应该是 NSAttributedStringKey.foregroundColor
Swift 4.2 更新:NSAttributedString.Key.foregroundColor 而不是 NSForegroundColorAttributeName
K
KlimczakM

要普遍更改颜色,此代码应位于 NavigationControllerviewDidLoad 函数中:

class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Status bar white font
        self.navigationBar.barStyle = UIBarStyle.Black
        self.navigationBar.tintColor = UIColor.whiteColor()
    }
}

要按 ViewController 更改它,您必须从 ViewController 引用 NavigationController 并在该 ViewControllerviewWillAppear 函数中编写类似的行。


如何覆盖 NavigationController 以使用这些方法?
您不会覆盖 NavigationController,只需在 ViewDidLoad 函数中对其进行修改。我在上面更新了我的答案。
所以开发人员只需将 NavigationController.swift 添加到工作区?如果是这样,那就太好了!
@AG1 这是不正确的,仅添加一个新文件 NavigationController.swift 是不够的,您必须将 Storyboard 中的 Navigation Controller 连接到 Identity Inspector 中的此文件,因为默认情况下它将使用通用 UINavigationController。
您也可以在情节提要中完成所有这些操作,而无需创建自定义导航控制器。 Bar Tine 和 Style 的属性就在属性检查器的右侧。
S
SwiftiSwift

斯威夫特 5

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

斯威夫特 4

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

我得到:类型'NSAttributedStringKey'(又名'NSString')没有成员'foregroundColor'
请使用以下代码获取最新版本的 xcode 10 和 swift 4.2 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
N
Nyakiba

要在 objective-c 中工作,我必须在我的 CustomViewController 中的 viewWillAppear 中添加以下几行。

[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];

对于 Swift2.x 这有效:

self.navigationController?.navigationBar.barTintColor = UIColor.redColor()

对于 Swift3.x 这有效:

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

但是当我回到另一个视图控制器时,颜色也发生了变化。关于如何防止这种情况的任何想法。我希望我的主视图控制器是一种颜色,而另一种是另一种颜色。
我认为您还必须在新视图控制器中设置颜色。
F
Fangming

在情节提要中完成这项工作(Interface Builder Inspector)

IBDesignable 的帮助下,我们可以为 UINavigationController 添加更多选项到 Interface Builder Inspector 并在情节提要上调整它们。首先,将以下代码添加到您的项目中。

@IBDesignable extension UINavigationController {
    @IBInspectable var barTintColor: UIColor? {
        set {
            navigationBar.barTintColor = newValue
        }
        get {
            guard  let color = navigationBar.barTintColor else { return nil }
            return color
        }
    }

    @IBInspectable var tintColor: UIColor? {
        set {
            navigationBar.tintColor = newValue
        }
        get {
            guard  let color = navigationBar.tintColor else { return nil }
            return color
        }
    }

    @IBInspectable var titleColor: UIColor? {
        set {
            guard let color = newValue else { return }
            navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: color]
        }
        get {
            return navigationBar.titleTextAttributes?["NSForegroundColorAttributeName"] as? UIColor
        }
    }
}

然后只需在情节提要上设置 UINavigationController 的属性。

https://i.stack.imgur.com/ZJsJX.gif


V
VirajP

如果要设置整个app的tint color和bar color,可以在AppDelegate.swift中添加如下代码

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

    var navigationBarAppearace = UINavigationBar.appearance()

    navigationBarAppearace.tintColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)
    navigationBarAppearace.barTintColor = UIColor(red:0.76, green:0.40, blue:0.40, alpha:1.0)
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    return true
`

Navigation barTintColor and tintColor is set


R
Raj Joshi

用 swift 4 更新

override func viewDidLoad() {
    super.viewDidLoad()
        self.navigationController?.navigationBar.tintColor = UIColor.blue
        self.navigationController?.navigationBar.barStyle = UIBarStyle.black
}

k
kuzdu

在 Swift5 和 Xcode 10 中

self.navigationItem.title = "your name"
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes

虽然此代码段可能会解决问题,但 including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。
R
Raksha.

斯威夫特 5.1

仅在 ViewDidLoad() 中复制和粘贴,并根据需要更改其大小。在复制和粘贴之前,在屏幕顶部添加导航栏。

navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "TitilliumWeb-Bold.ttf", size: 16.0)!, NSAttributedString.Key.foregroundColor: UIColor.white]

如果它不起作用,那么您可以尝试仅更改其文本颜色

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

A
Abhishek Jain

Swift 4.2 版本的 Albert 的回答——

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white]

我真的很喜欢你添加这个答案的方式。 .barTintColor 现在不是一个选项。你的意思是 .backgroundColor 吗?
R
Rawand Saeed

在 Swift 4.2 版本中将导航栏标题的文本颜色设置为白色:

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

V
Vladimir

斯威夫特 4

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.barTintColor = UIColor.orange
    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}

P
Patrick.Bellot

斯威夫特 4.1

添加一个函数到 viewDidLoad

override func viewDidLoad() {
  super.viewDidLoad()

  setup()
}   

setup() 函数中添加:

func setup() {

        navigationController?.navigationBar.prefersLargeTitles = true
        navigationController?.navigationBar.barStyle = .blackOpaque
        navigationItem.title = "YOUR_TITLE_HERE"
        navigationController?.navigationBar.barTintColor = .black
        let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        navigationController?.navigationBar.largeTitleTextAttributes = attributes
    }

R
Ryan Schaefer

对于 NavigationBar 处的 TitleText 自定义颜色,这里有一个 Swift 3 的简单而简短的代码:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

或者

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName :UIColor.white]

I
Imtee

在斯威夫特 4.2

var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]

b
bubbaspike

Swift 3.2(不是 Swift 4.0)

    self.navigationController?.navigationItem.largeTitleDisplayMode = .always
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

    // unconfirmed but I assume this works:
    self.navigationController?.navigationBar.barTintColor = UIColor.white
    self.navigationController?.navigationBar.barStyle = UIBarStyle.black

R
Ryan Schaefer

在 Swift 3 中这有效:

navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]