【问题标题】:UIBarButtonItem font reverts after navigationUIBarButtonItem 字体在导航后恢复
【发布时间】:2019-09-13 23:03:34
【问题描述】:

我正在使用 UINavigationController 的子类来设置 UIBarButtonItem 的外观。当导航堆栈第一次出现时,字体是正确的(在标有“项目”的按钮上)。但是,在导航到新屏幕后,新 UIBarButton 项(“Test”)的字体已恢复为默认外观。当我导航回原始 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)

    }

}

这是故事板的屏幕截图。

【问题讨论】:

    标签: ios uinavigationcontroller uikit


    【解决方案1】:

    虽然我确定这是 Apple 的一个错误,但我能够通过创建自定义 UINavigationBar 类并拦截栏按钮项来解决它。

    然后您只需要在故事板中设置 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)
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多