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

Kyl*_*lan 36 cocoa-touch uinavigationbar uinavigationcontroller ios swift

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

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

我认为这viewDidLoad会奏效:

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

但是leftBarButton?可选的退货nil.

Vex*_*exy 64

如果您需要在您的app(也就是每个导航按钮)中完全更改字体样式,首选方法是使用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)
Run Code Online (Sandbox Code Playgroud)

将此代码段放在AppDelegate.swift文件开头的某处是明智的,因为每次应用程序启动时都必须进行字体样式化.此外,您可以安全地将此代码放在您进行UI样式化和自定义的任何其他位置(例如,Presenter类).一旦执行此代码,之后将自定义所有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)
Run Code Online (Sandbox Code Playgroud)

现实世界的例子

在真正的应用程序中,您通常需要自定义两者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)
Run Code Online (Sandbox Code Playgroud)

}


使用appearance()代理的优点

  • 两个线性代码片段,用于在您的应用/项目中设置每个 样式UIBarButtonItem.
  • 您可以混合appearance()使用几个类的代理来获得真正独特的视觉风格
  • 如果您需要进一步的自定义控件,您可以进一步重新定制每个按钮(例如放置自定义视图,按钮,图像等) - 在特定的实例上UIBarButtonItem.

参考

appearance协议上的Apple Docs.


Lyn*_*ott 29

刚刚测试了你的代码,看起来行返回的原因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)
Run Code Online (Sandbox Code Playgroud)

编辑:从您在此答案下发布的评论中,似乎您实际上并不想设置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
Run Code Online (Sandbox Code Playgroud)


Mil*_*nia 21

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

斯威夫特4

我已经添加了这些行AppDelegate.swiftapplication功能:

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

斯威夫特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
}
Run Code Online (Sandbox Code Playgroud)

  • 还必须同时设置属性`for:.selected`,否则按钮将在按下时显示原始字体 (3认同)
  • 还需要 `for: .highlighted` 以在触地时保持相同的字体。 (2认同)

Ser*_*nov 6

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

  • 谢谢,我想知道为什么 setTitleTextAttribute 在我的 &gt; iOS13 代码中不起作用。`buttonAppearance.normal.titleTextAttributes = fontAttr` 就是我所需要的。 (2认同)