I am developing an app in Swift 2.2. Now I want to change the back button font and color for a certain view. The view in question has a navigation controller as it's parent controller.
我正在开发Swift 2.2中的应用程序。现在我想更改某个视图的后退按钮字体和颜色。有问题的视图有一个导航控制器作为它的父控制器。
I've tried running both of the following lines in viewDidLoad of my ViewController
我已尝试在ViewController的viewDidLoad中运行以下两行
self.navigationController!.navigationItem.backBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
self.navigationItem.backBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
Neither throws any errors, but it doesn't make any difference to the back button. I've also tried running both of these
不会抛出任何错误,但它对后退按钮没有任何影响。我也试过运行这两个
self.navigationController!.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
self.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
This however throws an error (error unwrapping nil). How should I change the font and color of the nav bar back button correctly? Feels like I'm not modifying the right items...
然而,这会引发错误(错误展开nil)。我该如何正确更改导航栏后退按钮的字体和颜色?感觉就像我没有修改正确的物品......
6 个解决方案
#1
12
If you want to set same color to bar buttons implicitly then in your appdelegate
in didfinishlaunchingwithoption
write,
如果你想隐式设置相同的颜色到条形按钮,那么在你的appdelegate中的didfinishlaunchingwithoption写入,
UINavigationBar.appearance().tintColor = UIColor.whiteColor() //your desired color here
Update :
更新:
put this in appdelegae,
把它放在appdelegae中,
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal) // your textattributes here
Update 2 :
更新2:
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.classForCoder()]).setTitleTextAttributes(["attribute" : "value"], forState: .Normal)
Hope this will help :)
希望这会有所帮助:)
#2
11
Swift 3.0 answer (based on Lion's answer):
Swift 3.0的答案(根据Lion的回答):
let newFont = UIFont(name: "Avenir Next", size: 16.0)!
let color = UIColor.white
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.classForCoder() as! UIAppearanceContainer.Type]).setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: newFont], for: .normal)
Works a treat, for those that have already managed to customise other parts of their nav bars but not the back button!
对于那些已经设法定制导航栏的其他部分而不是后退按钮的人来说,这是一种享受!
#3
4
I think you should change it in the vc before your actual vc. Look at: UINavigationItem
我认为你应该在实际的vc之前在vc中更改它。看看:UINavigationItem
Edit: For example you can write:
编辑:例如你可以写:
let item = UIBarButtonItem(title: "Text goes here", style: .Plain, target: self, action: #selector(self.navigationController?.popViewControllerAnimated(_:)))
item.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 23)!], forState: .Normal)
navigationItem.backBarButtonItem = item
in your prepareForSegue Method.
在您的prepareForSegue方法中。
#4
3
create custom button and make it as you want and add action to go back.
创建自定义按钮并根据需要进行设置,并添加操作以返回。
func addBackBarButtonOnNavigationBar(){
// add image here
let searchImage:UIImage = UIImage(named: "back button image")!
var backBtn:UIBarButtonItem = UIBarButtonItem(image: searchImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(classname.buttonActionMethodName(_:)))
backBtn.tintColor = UIColor.lightGrayColor()
if let font = UIFont(name: "AvenirNext", size: 15) {
backBtn.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
self.navigationItem.leftBarButtonItem = backBtn
}
func buttonActionMethodName(){
self.navigationController!.popViewControllerAnimated(true)
}
#5
2
Use the following code:
使用以下代码:
navigationController?.navigationBar.barTintColor = UIColor.purpleColor()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
change colour according to your need
根据您的需要改变颜色
#6
0
Swift 4
斯威夫特4
in AppDelegate.swift
在AppDelegate.swift中
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
#1
12
If you want to set same color to bar buttons implicitly then in your appdelegate
in didfinishlaunchingwithoption
write,
如果你想隐式设置相同的颜色到条形按钮,那么在你的appdelegate中的didfinishlaunchingwithoption写入,
UINavigationBar.appearance().tintColor = UIColor.whiteColor() //your desired color here
Update :
更新:
put this in appdelegae,
把它放在appdelegae中,
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal) // your textattributes here
Update 2 :
更新2:
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.classForCoder()]).setTitleTextAttributes(["attribute" : "value"], forState: .Normal)
Hope this will help :)
希望这会有所帮助:)
#2
11
Swift 3.0 answer (based on Lion's answer):
Swift 3.0的答案(根据Lion的回答):
let newFont = UIFont(name: "Avenir Next", size: 16.0)!
let color = UIColor.white
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.classForCoder() as! UIAppearanceContainer.Type]).setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: newFont], for: .normal)
Works a treat, for those that have already managed to customise other parts of their nav bars but not the back button!
对于那些已经设法定制导航栏的其他部分而不是后退按钮的人来说,这是一种享受!
#3
4
I think you should change it in the vc before your actual vc. Look at: UINavigationItem
我认为你应该在实际的vc之前在vc中更改它。看看:UINavigationItem
Edit: For example you can write:
编辑:例如你可以写:
let item = UIBarButtonItem(title: "Text goes here", style: .Plain, target: self, action: #selector(self.navigationController?.popViewControllerAnimated(_:)))
item.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 23)!], forState: .Normal)
navigationItem.backBarButtonItem = item
in your prepareForSegue Method.
在您的prepareForSegue方法中。
#4
3
create custom button and make it as you want and add action to go back.
创建自定义按钮并根据需要进行设置,并添加操作以返回。
func addBackBarButtonOnNavigationBar(){
// add image here
let searchImage:UIImage = UIImage(named: "back button image")!
var backBtn:UIBarButtonItem = UIBarButtonItem(image: searchImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(classname.buttonActionMethodName(_:)))
backBtn.tintColor = UIColor.lightGrayColor()
if let font = UIFont(name: "AvenirNext", size: 15) {
backBtn.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
self.navigationItem.leftBarButtonItem = backBtn
}
func buttonActionMethodName(){
self.navigationController!.popViewControllerAnimated(true)
}
#5
2
Use the following code:
使用以下代码:
navigationController?.navigationBar.barTintColor = UIColor.purpleColor()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
change colour according to your need
根据您的需要改变颜色
#6
0
Swift 4
斯威夫特4
in AppDelegate.swift
在AppDelegate.swift中
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)