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

192

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

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

}

输入图像描述

18个回答

333
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 中提到:

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


7
如果我们有多个导航栏,该怎么办? - sumit
9
tintColor在Xcode 7.3.1中无效,文本仍然是黑色。 - triiiiista
1
在Xcode 8.2中,titleTextAttributes不会自动完成,但它是有效的。 - oky_sabeni
barTintColor仍然必要吗?删除那行代码仍然可以工作。 - rgkobashi
这是添加文本属性,实际上并没有改变标题的颜色。如果您注意文本的字体,就可以理解它们之间的区别。 - Ram Madhavan
1
@Albert Vila Calvo 在 iOS 15 中这个东西不起作用了,你能告诉我怎么做吗? - Muhammad Danish Qureshi

130

我喜欢Alex的回答。如果你想在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
}

在此输入图片描述


但是我正在使用ISO 8.1。 - Saorikido
1
为什么要使用viewWillAppear()?我在viewDidLoad中这样做,它也能正常工作。 - Adam Johns
4
因为每次查看 UIView 时,您希望它都是设置好的。如果您在 viewDidLoad() 中进行设置,比如在 UITabBar 视图中,然后切换到另一个选项卡,在返回时它将被覆盖,因为原始视图只加载一次。 - FractalDoctor
2
Swift 4 的更新 - NSForegroundColorAttributeName 现在应该改为 NSAttributedStringKey.foregroundColor - Alan Scarpa
5
Swift 4.2 更新:使用 NSAttributedString.Key.foregroundColor 替代 NSForegroundColorAttributeName - Stephane Paquet
显示剩余3条评论

82
为了普遍改变颜色,此代码应位于 NavigationController viewDidLoad函数中:
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 来使用这些方法? - AG1
你不需要覆盖NavigationController,只需在ViewDidLoad函数中修改它即可。我已经更新了我的答案。 - Alex
1
那么开发人员只需要将 NavigationController.swift 添加到工作区中吗?如果是的话,那就很不错! - AG1
5
这是不正确的,仅添加一个新文件NavigationController.swift是不够的,您必须在Storyboard中的Identity Inspector中将您的导航控制器与此文件连接起来,因为默认情况下它将使用通用的UINavigationController。 - kakubei
2
您也可以在Storyboard中完成所有操作,而无需创建自定义导航控制器。在属性检查器下方的右侧,Bar Tine和Style的属性就在那里。 - Markymark

38

Swift 5

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

Swift 4

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

2
我得到了以下错误信息:类型 'NSAttributedStringKey'(又名 'NSString')没有成员 'foregroundColor'。 - Ahmadreza
请使用以下代码适用于最新版本的Xcode 10和Swift 4.2: self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] - Bhavsang Jam

17

为了在我的CustomViewController中使用Objective-C,我需要在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

但是当我返回到另一个视图控制器时,颜色也发生了变化。有什么办法可以防止这种情况发生吗?我希望我的主视图控制器是一种颜色,而其他视图控制器是另一种颜色。 - user3163404
1
我认为你还需要在你的新视图控制器中设置颜色。 - Niko Klausnitzer

15

在Storyboard(Interface Builder Inspector)中完成此任务

借助IBDesignable,我们可以为UINavigationController添加更多选项,并在Storyboard 上进行微调。首先,在项目中添加以下代码。

@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设置属性。

输入图像描述


8
在Swift5和Xcode 10中
self.navigationItem.title = "your name"
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes

虽然这段代码可能解决了问题,但包括解释真的有助于提高您发布的质量。请记住,您是在为未来的读者回答问题,而这些人可能不知道您代码建议的原因。 - 31piy

7

如果你想设置整个应用的色调和状态栏颜色,可以在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
`

导航栏的背景颜色和图标颜色已设置


6

已更新至Swift 4

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

5

Swift 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]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接