如何更改导航栏后退按钮的字体?

时间:2014-12-20 18:58:32

标签: ios swift cocoa-touch uinavigationcontroller uinavigationbar

如何更改导航栏的后退按钮的字体。

后退按钮是"后退"或前一个视图控制器的标题。

我认为viewDidLoad可行:

navigationController?.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FONTNAME", size: 20)!], forState: UIControlState.Normal)

leftBarButton?可选返回nil

6 个答案:

答案 0 :(得分:62)

如果您需要在应用程序(也就是每个导航按钮)中更改完全字体样式,首选方法是使用UIBarButtonItem.appearance()代理。

示例代码段如下所示:

SWIFT 3.0 +

//make sure font name u use below *actually* exists in the system !
//if it doesn't app will crash because we're force unwrapping an optional (see below) !!!
let customFont = UIFont(name: "customFontName", size: 17.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)

明智的做法是将此代码段放在AppDelegate.swift文件开头的某处,因为每次应用程序启动时都必须进行字体样式化。此外,您可以安全地将此代码放在任何其他位置(例如。Presenter类)中,您可以在其中进行UI样式化和自定义。一旦执行此代码,此后将自定义所有BarButton。


但是作为一个真正的Swift,你应首先自行解开你的字体并最终回退到系统字体,如果找不到它或在字体加载过程中出现任何其他可能的问题。

var textAttributes: [String:Any]

//custom font setup
let fontColor = UIColor.purple
let barItemCustomFont = UIFont(name: "", size: 14)  //note we're not forcing anything here

//can we use our custom font 
if let customFont = barItemCustomFont {
    //hooray we can use our font 
    textAttributes = [NSForegroundColorAttributeName: fontColor, NSFontAttributeName: customFont]
} else {
    // not found -> omit setting font name and proceed with system font
    textAttributes = [NSForegroundColorAttributeName: fontColor]
}

//finally
UIBarButtonItem.appearance().setTitleTextAttributes(textAttributes, for: .normal)

真实世界的例子

在真实应用中,您通常需要自定义UINavigationBarUIBarButton字体样式(除了其他参数)以使它们在视觉上保持一致。 您可以使用以下方便的stylizeNavigationFontOfMyAmazingApp功能:

func stylizeNavigationFontOfMyAmazingApp() {
    //custom font
    let customFont = UIFont(name: "someFancyFont", size: 16)!  //note we're force unwrapping here

    //navigation bar coloring:
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().barTintColor = UIColor.blue

    //unique text style:
    var fontAttributes: [String: Any]
    fontAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: customFont]

    //navigation bar & navigation buttons font style:
    UINavigationBar.appearance().titleTextAttributes = fontAttributes
    UIBarButtonItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)

}


使用appearance()代理

的优点
  • 两个线性代码段,用于样式化每个 UIBarButtonItem 你的app / project。
  • 您可以混合多个类的appearance()个代理,以获得真正独特的视觉风格
  • 如果您需要进一步的自定义控制,您可以在UIBarButtonItem的特定实例上进一步重新定制每个按钮(例如,放置自定义视图,按钮,图像等)。

参考

appearance协议上的

Apple Docs

答案 1 :(得分:27)

刚刚测试了您的代码,似乎行返回nil的原因实际上是因为name: "FONTNAME"返回nil。因此,如果将name属性设置为有效的字体名称,则代码应该在没有错误的情况下运行 - 即使navigationController?.navigationItem.leftBarButtonItem显式设置为nil。

但无论如何,正如我通过测试所看到的那样,这条线不会给你你想要的结果。领先的navigationController可选项不应该存在,因为它访问您的UINavigationController而不是当前视图。只需使用UIViewController自己的navigationItem媒体资源直接访问leftBarButtonItem,例如:

let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)

修改:根据您在此回答中发布的评论,您好像不想设置leftBarButtonItem而是设置backBarButtonItem,因为您除了回到上一个视图控制器之外,我们不想实现自定义操作。

因此,在之前的视图控制器(即您想要显示自定义后退按钮项之前的视图)中,您可以设置自定义后退按钮而不执行如下操作:

let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
backButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)
navigationItem.backBarButtonItem = backButton

答案 2 :(得分:20)

如果您想将此更改应用于所有应用程序,可以使用以下代码:

Swift 4

我已将这些行添加到AppDelegate.swift函数中的application

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.font: UIFont(name: "IranSansMobile", size: 20)!
        ]

        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)

  return true
}

Swift 3

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    UINavigationBar.appearance().titleTextAttributes = [
        NSFontAttributeName: UIFont(name: "IranSansMobile", size: 20)!
    ]

    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)


    return true
}

答案 3 :(得分:3)

如果您确定已经设置了UIBarButtonItem,则可以执行以下操作:

self.navigationItem.leftBarButtonItem!.title = "myTitle"

要改变,例如您可以执行以下操作的颜色:

self.navigationItem.leftBarButtonItem!.tintColor = UIColor.redColor()

如果你没有在故事板中设置它,你可以这样做:

self.navigationItem.leftBarButtonItem = UIBarButtonItem()

要更改字体,请执行以下操作:

self.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FONTNAME", size: 20)!], forState: .Normal)

也许我应该提一下你不能使用self。navigationController因为在设置navigationController的按钮时它们不会显示在屏幕上,因此屏幕上的按钮已被设置为故事板必须是self.navigationItem ...

答案 4 :(得分:2)

SWIFT 3

    UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Open Sans", size: 15)!,NSForegroundColorAttributeName: UIColor.white]
    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Open Sans", size: 15)!], for: UIControlState.normal)

答案 5 :(得分:2)

对于iOS 13,Swift 5.1:

let fontAttr = [NSAttributedString.Key.font: \*your font*\]

let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = fontAttr

let navbarAppearance = UINavigationBarAppearance()
navbarAppearance.buttonAppearance = buttonAppearance
UINavigationBar.appearance().standardAppearance = navbarAppearance