如何更改UINavigationBar标题的字体和文本颜色

29

我希望将标题的字体更改为Avenir,并使其变为白色。这是我在viewDidLoad方法中的代码:

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

这段代码仅改变标题的颜色而不改变字体。此外,我尝试将其实现在AppDelegate文件中,但并没有起作用。

这是我的UINavigationBar的样子:进入图像描述

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

7个回答

37

看起来这也是必要的:

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]

14
如果您想在整个应用程序中应用这些样式,请在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];
希望这能帮助您的问题 :)

14

如果有人需要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)!]

1
UINavigationBar.appearance().tintColor在Xcode 10和Swift 5中不会影响我的导航标题文本颜色,它们仍然是黑色的。我找不到如何使用我的主题更新它们。 - Vilmir
2
@Vilmir,你应该使用titleTextAttributes来设置标题。tintColor是用于UIBarButtonItems的。 - AlexanderZ

6

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

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

5
在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)!]

这里的代码用于标题和大标题。如果您想使用系统字体,也可以删除自定义字体。


3

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

因此,在主要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'可以设置标题颜色、色调、NavBar颜色等。standardAppearance是在加载时显示的。

对于iOS15,我花了一些时间才弄清楚这个问题。

不客气! :-)


1
我会创建一个插座并执行以下操作:

@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)!]
}

感谢您的回复!我已经通过编程方式创建了所有的 UI 元素,没有使用 Interface Builder。因此,这种解决方案并不适合我的需求。 - Rehaan Advani

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