This is surprisingly difficult! The best I came up with is a subclass of UITabBarController , and then do it in init :
super.init(nibName: nil, bundle: nil) object_setClass(self.tabBar, CustomTabBar.self) (self.tabBar as? CustomTabBar)?.setup()
Unfortunately, you cannot set the class before calling super.init (not in Swift anyway), and therefore, by the time the class is init , the init method is already running and therefore will not be called in your custom subclass. To get around this, I just added the setup() method to complete all my settings.
Another option in Swift is to extend the UITabBar and do something like this:
extension UITabBar { open override func willMove(toSuperview newSuperview: UIView?) { super.willMove(toSuperview: newSuperview)
However, this will affect all instances of UITabBar , so I prefer the first option.
Luke rogers
source share