gpt4 book ai didi

ios - UIBarButtonItem 字体在导航后恢复

转载 作者:行者123 更新时间:2023-11-29 05:28:17 24 4
gpt4 key购买 nike

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

这是一个错误还是我做错了?

这是我的子类。

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)

}

}

这是 Storyboard的屏幕截图。

the storyboard

最佳答案

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

然后,您只需在 Storyboard中设置 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)
}

}

}

关于ios - UIBarButtonItem 字体在导航后恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57931204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com