Ios UIBarButtonItem字体在导航后恢复

Ios UIBarButtonItem字体在导航后恢复,ios,uinavigationcontroller,uikit,Ios,Uinavigationcontroller,Uikit,我正在使用UINavigationController的子类来设置UIBarButtonItem的外观。当导航堆栈首次出现时,字体是正确的(在标记为“项”的按钮上)。但是,导航到新屏幕后,新UIBarButton项(“测试”)的字体已恢复为默认外观。当我向后导航时,原始UIBarButton项(“项”)也恢复为默认外观 这是一个错误还是我做得不对 这是我的子类 import UIKit open class CustomNavigationController: UINavigationCon

我正在使用UINavigationController的子类来设置UIBarButtonItem的外观。当导航堆栈首次出现时,字体是正确的(在标记为“项”的按钮上)。但是,导航到新屏幕后,新UIBarButton项(“测试”)的字体已恢复为默认外观。当我向后导航时,原始UIBarButton项(“项”)也恢复为默认外观

这是一个错误还是我做得不对

这是我的子类

import UIKit

open class CustomNavigationController: UINavigationController {

    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        self.style()
    }

    required public init?(coder: NSCoder) {
        super.init(coder: coder)
        self.style()
    }

    override public init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?) {
        super.init(navigationBarClass: navigationBarClass, toolbarClass: toolbarClass)
        self.style()
    }


    override public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.style()
    }

    func style() {

        let color = UIColor.green
        let font = UIFont.init(name: "Zapfino", size: 18)!

        let attributes: [AnyHashable : Any] = [
            NSAttributedString.Key.foregroundColor : color,
            NSAttributedString.Key.font : font
                       ]
        UIBarButtonItem
            .appearance(whenContainedInInstancesOf: [CustomNavigationController.self])
            .setTitleTextAttributes((attributes as! [NSAttributedString.Key : Any]), for: .normal)

        let highlightedAttributes: [AnyHashable : Any] = [
            NSAttributedString.Key.foregroundColor : color,
            NSAttributedString.Key.font : font
        ]

        UIBarButtonItem
            .appearance(whenContainedInInstancesOf: [CustomNavigationController.self])
            .setTitleTextAttributes((highlightedAttributes as! [NSAttributedString.Key : Any]), for: .highlighted)

    }

}
这是故事板的截图


虽然我确信这是Apple的一个bug,但我还是能够通过创建自定义UINavigationBar类并截取bar按钮项来解决它

然后,您只需要在情节提要中设置UINavigationBar的类,或者使用
UINavigationController:initWithNavigationBarClass

class CustomNavigationBar: UINavigationBar {

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    func color() -> UIColor { return UIColor.green }
    func font() -> UIFont { return UIFont.init(name: "Zapfino", size: 18)! }

    func attributes() -> [NSAttributedString.Key : Any] {
        return [
            NSAttributedString.Key.foregroundColor : self.color(),
            NSAttributedString.Key.font : self.font()
        ]
    }

    let appearance =  UIBarButtonItem.appearance()

    override var backItem: UINavigationItem? {
        let item = super.backItem
        item?.backBarButtonItem?.setTitleTextAttributes(attributes(), for: .normal)
        item?.rightBarButtonItem?.setTitleTextAttributes(attributes(), for: .normal)

        return item
    }

    override var topItem: UINavigationItem? {
        let item = super.topItem
         item?.backBarButtonItem?.setTitleTextAttributes(attributes(), for: .normal)
        item?.rightBarButtonItem?.setTitleTextAttributes(attributes(), for: .normal)
        return item
    }

    override func setItems(_ items: [UINavigationItem]?, animated: Bool) {

        super.setItems(items, animated: animated)

        guard let items = items else { return }

        for item in items  {
            item.backBarButtonItem?.setTitleTextAttributes(attributes(), for: .normal)
        }

    }

}