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

Reh*_*ani 23 fonts uinavigationbar uinavigationcontroller ios swift

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

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

此代码仅更改标题的颜色,而不是字体.另外,我尝试在App Delegate File中实现它,但这不起作用.

这是我的UINavigationBar的样子:在此输入图像描述

我希望标题也有Avenir的字体.我该如何实现这一目标?

Fre*_*ust 32

我用这个:

更新,似乎也需要这样:

    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]
Run Code Online (Sandbox Code Playgroud)


Lak*_*aky 11

如果您希望在整个应用程序中应用样式,请在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 
}];
Run Code Online (Sandbox Code Playgroud)

NSForegroundColorAttributeName设置字体颜色.

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

NSShadowAttributeName对文本应用了漂亮的阴影效果,如果需要,可以删除此部分.

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

[[UIBarButtonItem appearance]
     setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000)
     forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

希望这有助于你的问题:)


alg*_*rid 8

万一有人需要在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)!]
Run Code Online (Sandbox Code Playgroud)

  • @Vilmir 你应该使用“titleTextAttributes”作为标题。`tintColor` 用于 UIBarButtonItems。 (2认同)

Mil*_*nia 6

您的代码没问题,您只需要将所有内容设置为titleTextAttributes一行:

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


小智 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)!]
Run Code Online (Sandbox Code Playgroud)

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