2016-10-19 41 views
0

我有下面的代码,它在将我的应用程序更新到Swift3之前曾经工作过。在iOS 10中更改UIBarButtonItem字体

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() 

它应该改变导航栏项目的字体和颜色,但它不工作了。问题不在于字体,因为它在整个应用程序中都显示正确。有任何想法吗?

+0

您的代码适用于xcode 8中的我。我能够看到ui barbutton的文本颜色更改。 –

+0

代码updated.take ... – Joe

+0

@NaveenRamanathan奇怪。您是否使用自定义字体进行尝试? – smeshko

回答

0

试试这个代码:在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 

更新:

的AppDelegate:从自定义代码

UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Villa", size: 25.0)!], for: UIControlState.normal) 

输出字体:

enter image description here

+0

正如你可以在我的代码中看到的,我试图改变所有'UIBarButtonItem'的外观,所以我没有访问具体参考。 – smeshko

+0

@smeshko是你想要什么...干杯 – Joe

+0

@smeshko好吧,我会看到。谢谢你让我知道... – Joe