我需要更改 AppDelegate
中的 UINavigationBar 后退栏按钮文本,以将更改应用到我应用中的所有 Views
。
我已经使用以下方式更改了标题字体样式:
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MyCustomFont", size: 20)!
]
但我不知道如何访问左侧栏按钮以对其进行更改。
最佳答案
swift 3.0,4.0
只需使用UINavigationItem
的extension
即可实现。根据许多搜索,无法使用应用程序委托(delegate)更改左键文本。
extension UINavigationItem{
override open func awakeFromNib() {
super.awakeFromNib()
let backItem = UIBarButtonItem()
backItem.title = "Hello"
if let font = UIFont(name: "Copperplate-Light", size: 32){
backItem.setTitleTextAttributes([NSFontAttributeName:font], for: .normal)
}else{
print("Font Not available")
}
/*Changing color*/
backItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
self.backBarButtonItem = backItem
}
}
更新:
您可以在 didFinishLaunchingWithOptions
上从 AppDelegate
更改后退按钮箭头颜色,
/*It will change back arrow color only if you use backItem.setTitleTextAttributes, else it will change whole text color*/
UINavigationBar.appearance().tintColor = UIColor.orange
关于ios - 使用 Swift 从 AppDelegate 更改 UINavigationBar 后退按钮文本和字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45650840/