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

如何更改我的导航栏的后退button的字体。

后退button是“返回”或从前一个视图控制器的标题。

我认为这viewDidLoad将工作:

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

但是leftBarButton? 可选返回nil

刚刚testing过你的代码,看起来line是返回nil的原因其实是因为name: "FONTNAME"返回nil。 因此,如果将该name属性设置为有效的字体名称,那么即使navigationController?.navigationItem.leftBarButtonItem显式设置为nil,代码也应该运行而不会出现错误。

但是无论如何,正如我通过testing所看到的,这条线不会给你你显然想要的结果。 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因为你不想实现一个自定义操作,除了回到以前的视图控制器。

因此,在以前的视图控制器(即您想显示您的自定义后退button项目之前的视图),您可以设置您的自定义后退button,而不需要这样的操作:

 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 

如果你需要完全改变你的应用程序(也就是每个导航button)的字体样式,首选的方法是使用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将在之后被自定义。


要保持真正的🤓you,首先应该select性地打开字体,并最终回退到系统字体(如果未find)

 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进行风格化。
  • 你可以混合几个类的appearance()代理来获得真正独特的视觉风格
  • 如果您需要进一步的自定义控制,您可以在UIBarButtonItem特定实例上进一步重新定制每个button(例如,放置自定义视图,button,图像等)。

– 参考 –

关于appearance协议的Apple Docs 。

如果你想把这个改变应用到你所有的应用程序中,你可以使用下面的代码:

斯威夫特4

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

 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 } 

当你确定你已经设置了一个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) 

也许我应该提到你不能用自己。 navigationController因为当设置导航self.navigationItem的button,他们不显示在屏幕上,因此在屏幕上,并已设置在故事板中的button必须是self.navigationItem

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)