If I added the code in viewDidLoad() then I was never able to change the font when the tabbar was selected.
This is a great article that explains how to do it with more details: HolySwift Article
In a nutshell, you need to add the following code in your tabbar controller:
override var selectedIndex: Int {
didSet {
guard let selectedViewController = viewControllers?[selectedIndex] else {
return
}
selectedViewController.tabBarItem.setTitleTextAttributes([.font: UIFont.boldSystemFont(ofSize: 13)], for: .normal)
}
}
And this:
override var selectedViewController: UIViewController? {
didSet {
guard let viewControllers = viewControllers else {
return
}
for viewController in viewControllers {
if viewController == selectedViewController {
viewController.tabBarItem.setTitleTextAttributes([.font: UIFont.boldSystemFont(ofSize: 13)], for: .normal)
} else {
viewController.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 12)], for: .normal)
}
}
}
}
PS: This will work with custom fonts as well.