在iOS 10中更改UIBarButtonItem字体

时间:2016-10-19 08:55:17

标签: ios swift

我有以下代码,它在将我的应用程序更新到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()

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

1 个答案:

答案 0 :(得分: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