UIKit updates the badge font sometime after the view's layoutSubviews
or viewWillAppear
. Fully overriding this will need a bit of code. You want to start by observing the badge's font change.
tabBarItem.addObserver(self, forKeyPath: "view.badge.label.font", options: .new, context: nil)
Now once the observe method is called it's safe to set the badge font. There's one catch however. UIKit wont apply the same change twice. To get around this issue first set the badge attributes to nil and then reapply your font.
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "view.badge.label.font" {
let badgeAttributes = [NSFontAttributeName: UIFont(name: "IRANSans", size: 14)]
tabBarItem?.setBadgeTextAttributes(nil, for: .normal)
tabBarItem?.setBadgeTextAttributes(badgeAttributes, for: .normal)
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
Just incase of future iOS updates, you might want to wrap the addObserver
in a try-catch. Also don't forget to remove the observer when your done!