Swift 更改后退按钮的字体和颜色
Swift change font and color of back button
我正在 Swift 2.2 中开发应用程序。现在我想更改某个视图的后退按钮字体和颜色。有问题的视图有一个导航控制器,因为它是父控制器。
我已经在 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)
两者都没有抛出任何错误,但对后退按钮没有任何影响。我也试过 运行 这两个
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)
然而,这会引发错误(展开 nil 时出错)。我应该如何正确更改导航栏后退按钮的字体和颜色?感觉我没有修改正确的项目...
使用以下代码:
navigationController?.navigationBar.barTintColor = UIColor.purpleColor()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
根据需要更改颜色
创建自定义按钮并根据需要制作它并添加返回操作。
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)
}
如果您想隐式地为条形按钮设置相同的颜色,那么在您的 AppDelegate
中,在 didfinishlaunchingwithoptions
中,写:
UINavigationBar.appearance().tintColor = UIColor.white //your desired color here
更新:
把这个放在 AppDelegate
,
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal) // your textattributes here
更新 2:
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.classForCoder()]).setTitleTextAttributes(["attribute" : "value"], forState: .Normal)
希望这会有所帮助:)
我认为您应该在实际 vc 之前的 vc 更改它。看:UINavigationItem
编辑:
例如你可以这样写:
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
在您的 prepareForSegue 方法中。
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)
对于那些已经设法自定义导航栏的其他部分而不是后退按钮的用户来说,这是一种享受!
Swift 4
在AppDelegate.swift
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
在swift4.2
to change back button color
self.navigationController?.navigationBar.tintColor = .white
在 Swift 5 你可以通过这些来做到:
self.navigationItem.backBarButtonItem?.tintColor = UIColor.red
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular)]
self.navigationItem.backBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
请注意,它对 下一个 推送的视图控制器有效,而不是显示器上的当前视图控制器,这就是为什么它非常混乱!
此外,检查故事板和 select previous 视图控制器的导航项,然后在 后退按钮 (检查员)。
我正在 Swift 2.2 中开发应用程序。现在我想更改某个视图的后退按钮字体和颜色。有问题的视图有一个导航控制器,因为它是父控制器。
我已经在 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)
两者都没有抛出任何错误,但对后退按钮没有任何影响。我也试过 运行 这两个
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)
然而,这会引发错误(展开 nil 时出错)。我应该如何正确更改导航栏后退按钮的字体和颜色?感觉我没有修改正确的项目...
使用以下代码:
navigationController?.navigationBar.barTintColor = UIColor.purpleColor()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
根据需要更改颜色
创建自定义按钮并根据需要制作它并添加返回操作。
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)
}
如果您想隐式地为条形按钮设置相同的颜色,那么在您的 AppDelegate
中,在 didfinishlaunchingwithoptions
中,写:
UINavigationBar.appearance().tintColor = UIColor.white //your desired color here
更新:
把这个放在 AppDelegate
,
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal) // your textattributes here
更新 2:
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.classForCoder()]).setTitleTextAttributes(["attribute" : "value"], forState: .Normal)
希望这会有所帮助:)
我认为您应该在实际 vc 之前的 vc 更改它。看:UINavigationItem
编辑: 例如你可以这样写:
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
在您的 prepareForSegue 方法中。
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)
对于那些已经设法自定义导航栏的其他部分而不是后退按钮的用户来说,这是一种享受!
Swift 4
在AppDelegate.swift
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
在swift4.2
to change back button color
self.navigationController?.navigationBar.tintColor = .white
在 Swift 5 你可以通过这些来做到:
self.navigationItem.backBarButtonItem?.tintColor = UIColor.red
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular)]
self.navigationItem.backBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
请注意,它对 下一个 推送的视图控制器有效,而不是显示器上的当前视图控制器,这就是为什么它非常混乱!
此外,检查故事板和 select previous 视图控制器的导航项,然后在 后退按钮 (检查员)。