如何更改导航栏的后退按钮的字体。
后退按钮是"后退"或前一个视图控制器的标题。
我认为viewDidLoad
可行:
navigationController?.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FONTNAME", size: 20)!], forState: UIControlState.Normal)
但leftBarButton?
可选返回nil
。
答案 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)
在真实应用中,您通常需要自定义UINavigationBar
和UIBarButton
字体样式(除了其他参数)以使它们在视觉上保持一致。
您可以使用以下方便的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
协议上的
答案 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