我有以下代码,该代码在将我的应用程序更新到 Swift3 之前可以正常工作。
let barButtonsAppearanceDictionary: [String : AnyObject] = [NSFontAttributeName : UIFont.tangraText(withSize: 17)!,
NSForegroundColorAttributeName : UIColor.tangraBlue()]
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonsAppearanceDictionary, for: UIControlState())
let barButtonsDisabledAppearanceDictionary: [String : AnyObject] = [NSFontAttributeName : UIFont.tangraText(withSize: 17)!,
NSForegroundColorAttributeName : UIColor.lightGray]
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonsDisabledAppearanceDictionary, for: .disabled)
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.tangraBlue(),
NSFontAttributeName : UIFont.tangraText(withSize: 22)!]
UINavigationBar.appearance().tintColor = UIColor.tangraBlue()
它应该更改导航栏项目的字体和颜色,但它不再起作用了。问题不在于字体,因为它在整个应用程序中正确显示。有什么想法吗?
最佳答案
尝试此代码:在 Xcode 8 中测试:
在viewdidLoad中:
navigationController?.toolbar.setBackgroundImage(UIImage(), forToolbarPosition: UIBarPosition.any, barMetrics: UIBarMetrics.default)
navigationController?.toolbar.barStyle = UIBarStyle.default
navigationController?.toolbar.tintColor = UIColor.red
navigationController?.toolbar.backgroundColor = UIColor.green
更新:
应用程序委托(delegate):
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Villa", size: 25.0)!], for: UIControlState.normal)
使用自定义字体的代码的输出:
关于ios - 在 iOS 10 中更改 UIBarButtonItem 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40126571/