我知道这必须简单,但它让我不知所措:
我在UINavigationController中有代码,它可以很好地设置导航栏标题的字体,以及背景颜色和导航项的颜色:
override func viewDidLoad() {
super.viewDidLoad()
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.white
navigationBarAppearace.barTintColor = UIColor(colorLiteralRed: 46/255, green: 99/255, blue: 201/255, alpha: 1.0)
navigationBarAppearace.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 17)!]
}
但我无法获取要更改的导航项的字体。我尝试过一些东西,包括(在同一个viewDidLoad中):
self.navigationItem.backBarButtonItem?.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "OpenSans", size: 17)!], for: UIControlState.normal)
以及(使用导航栏的其中一个ViewControllers的viewDidLoad中)
self.navigationController?.navigationItem.backBarButtonItem?.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "OpenSans", size: 17)!], for: UIControlState.normal)
我在plist中有字体,并且在项目的其他部分成功使用它。我想我只是想以错误的方式做这件事,有人可以帮忙吗?
答案 0 :(得分:2)
您可以在UINavigationViewController类中更改所有UIBarButtonItem的外观:
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font!], for: UIControlState.normal)
答案 1 :(得分:0)
如果OpenSans
不在plist文件中,您将获得fatal error: unexpectedly found nil while unwrapping an Optional value
所以试试
override func awakeFromNib() {
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.white
navigationBarAppearace.barTintColor = UIColor(colorLiteralRed: 46/255, green: 99/255, blue: 201/255, alpha: 1.0)
navigationBarAppearace.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 17)!]
}
difference between awakeFromNib() and viewDidLoad() in swift