I can create a UINavigationController
with custom bar classes by using initWithNavigationBarClass:toolbarClass:
. There doesn't seem to be an equivalent for UITabBarController
, so how do I get it to use a custom UITabBar
class?
Every solution I've seen so far is unsuitable because either
- It uses IB
- It adds a second tab bar to the
UITabBarController
instead of changing its existing one, or - It throws
UITabBarController
away and makes a new controller class.
I want a real UITabBarController
created in code using a custom class for its tab bar. How do I achieve this?
This is surprisingly hard! The best I've come up with is subclassing
UITabBarController
and then doing this in theinit
:Unfortunately you can't set the class before the call to
super.init
(not in Swift anyway), and so by the time you change the class theinit
method has already been run and so won't be called on your custom subclass. To get around this, I've just added asetup()
method to do all my customisation in.Another option in Swift is to extend
UITabBar
and do something like this:However this will affect all instances of
UITabBar
, so I prefer the first option.I do not think it is possible.
Here is the Apple Documentation that talks about the
tabBar
property ofUITabBarController
.As far as I know you can't do this. Your best option to do this without IB is to have your own UIViewController (not subclassing UITabBarController) and then add your own subclass of UITabBar to that Controller.
You may also want to review the Controller Hierarchy if you decide to follow this approach.