修改UINavigationBar标题字体和文字颜色的方法

How to change the font and text color of the title of UINavigationBar

我想把标题的字体改成Avenir,我想把它变成白色。这是我在 viewDidLoad 方法中的代码:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 20)!]
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

这段代码只改变标题的颜色,不改变字体。此外,我尝试在 App Delegate 文件中实现它,但这没有用。

这是我的 UINavigationBar 的样子:

我希望标题也有 Avenir 字体。我该如何实现?

看来也需要这个:

self.navigationController?.navigationBar.barStyle       = UIBarStyle.Black // I then set the color using:

self.navigationController?.navigationBar.barTintColor   = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0) // a lovely red

self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() // for titles, buttons, etc.

let navigationTitleFont = UIFont(name: "Avenir", size: 20)!

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: navigationTitleFont]

我只想创建一个出口并执行此操作:

@IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 30)!]
}

如果您希望在整个应用程序中应用样式,请在 AppDelegate.m 文件中添加这些行。

NSShadow* shadow = [NSShadow new]; 
shadow.shadowOffset = CGSizeMake(0.0f, 0.0f); 
shadow.shadowColor = [UIColor blackColor]; 

[[UINavigationBar appearance] setTitleTextAttributes: @{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:@"Kelvetica Nobis" size:20.0f],
NSShadowAttributeName: shadow 
}];

NSForegroundColorAttributeName 设置字体颜色。

NSFontAttributeName 为标题文本设置自定义字体。

NSShadowAttributeName 给文字加上了漂亮的阴影效果,如果需要可以去掉这部分。

另外,您可以提前添加这些行,以隐藏当您导航到导航控制器中的另一个视图时出现在操作栏后退按钮中的文本。

[[UIBarButtonItem appearance]
     setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000)
     forBarMetrics:UIBarMetricsDefault];

希望这对您的问题有所帮助:)

你的代码没问题你只需要在一行中设置所有titleTextAttributes:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white , NSFontAttributeName: UIFont(name: "Avenir", size: 17)!]

以防有人在 Swift 4:

中需要这个
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.barTintColor = .red
navBarAppearance.tintColor = .white
navBarAppearance.titleTextAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]

Swift 5:

let appearance = UINavigationBarAppearance()
    appearance.titleTextAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
    appearance.largeTitleTextAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont(name: "Avenir", size: 35)!]

此处的代码用于标题和大标题。如果你想要系统字体,你也可以删除自定义字体。

我正在为 iOS 15 更新此内容。如果您有一个将某些内容推到导航栏后面的滚动视图,如果您不设置“navigationBar.scrollEdgeAppearance”,它将更改导航栏背景。

所以这里有一个简单的配置,可以为您的主 ViewController.

的 ViewDidLoad 中的所有视图设置它
        // Set top Nav Bar behavior for ALL of app
    let standardAppearance = UINavigationBarAppearance()
    
    // Title font color
    standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
    // prevent Nav Bar color change on scroll view push behind NavBar
    standardAppearance.configureWithOpaqueBackground()
    standardAppearance.backgroundColor = UIColor.blue
    
    
    self.navigationController?.navigationBar.standardAppearance = standardAppearance
    self.navigationController?.navigationBar.scrollEdgeAppearance = standardAppearance

因此'scrollEdgeAppearance'您可以设置标题颜色、色调、导航栏颜色和作品。 standardAppearance 是加载时显示的内容。

我花了一些时间才弄清楚 iOS15。

不客气! :-)