5

Basically I want to change the height of the tab bar, and to do so, I've found multiple solutions that works by subclassing the UITabBar.

However to actually use that subclass in a tab controller they (the people in other SO threads) seem to tell me to change it (the subclass) in the storyboard. But the app I am working creates the tab controller programmatically, so I am therefore unable to change the subclass in storyboard.

Here is an example of a SO question where the solution is posted, however it uses the storyboard to use the new tab bar controller subclass.

So. Is there a way to use my subclassed UITabBar in my UITabBarController?


For example. Let's assume I take the solution from this thread changing the height of UITabBar in iOS7/8?

@implementation MyTabBar

-(CGSize)sizeThatFits:(CGSize)size
{
    CGSize sizeThatFits = [super sizeThatFits:size];
    sizeThatFits.height = 100;

    return sizeThatFits;
}

@end

Now. How can I use this class (MyTabBar) as the tab bar of choice in my programmatically built UITabBarController.

The first thought I had was to do something like

self.tabBar = [[MyTabBar alloc] init];

But the property is readonly.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jontelang
  • 589
  • 4
  • 17
  • @Sneak Sorry, I've added more information. And the link you posted is not related to the issue I am having. – jontelang Mar 17 '17 at 08:21
  • My only question is -- how can I change the class of the tabBar property on a UITabBarController, programatically. No more, no less. – jontelang Mar 17 '17 at 08:22
  • If you look at the link I provided - subclassing the UITabBar itself does work. But it requires a storyboard. My question is simply if I can do what they do only without the storyboard requirement. – jontelang Mar 17 '17 at 09:30
  • My question is IF it is POSSIBLE to do it. I am currently using the way you are describing in my solution. But I am curious if it works, because it feels cleaner than changing the frame. Hence the question itself. – jontelang Mar 17 '17 at 10:11
  • Of course it is an extension of an UIView. Subclass it, and then as I said again, subclass a custom "UITabBarController" and override it inside your custom class as the link I provided, to do the work that UITabBarController already does for you, with your subclass of a UITabBar and implement all the delegate methods as provided by the link. However, as I said, I don't see the reason you would want to do this when you have been handed a UITabBarController to do this work for you. Also, your question is regarding the height. –  Mar 17 '17 at 10:17
  • Where exactly does your link state that I can exchange the UITabBar class for MyTabBarClass programatically? I don't see it. Subclassing the controller means I have to manually change the frame from the vc. It works, but it is NOT the answer to my question. I am wondering how I can get the VC to use my custom class. I am not asking how to modify the frame in a VC subclass. – jontelang Mar 17 '17 at 10:35
  • Quoted: "When creating a tab bar programmatically, use the init(frame:​) method or another view initializer method to set its initial configuration. Use the methods of this class to configure the appearance of the tab bar. For tab bars you create yourself, you also use the methods of this class to specify the items displayed by the tab bar." I don't understand a word . First you want to know how to change the height, then you want to know how to use the VC , then you want to know how to subclass. I have answered **all three questions**, cba to waste more time. GL –  Mar 17 '17 at 10:42
  • I never asked about the height, my first sentence says that I already found the solution to that. The init method works for subclasses, but it doesn't let me actually use this class as the "self.tabBar" in my UITabBarController. I also don't care about the items within it. My ONLY question is -- and I repeat -- if I can I assign my subclasses UITabBar to be the object my UITabBarController has in its self.tabBar property. The answer seems to be NO. I'm sorry but none of your suggestion have answered my question - probably because it is not possible. – jontelang Mar 17 '17 at 14:52
  • https://developer.apple.com/reference/uikit/uitabbarcontroller/1621174-tabbar?language=objc You don't manipulate an actual property of the UITabBarController, it is made to use out of box for you. Again, **you need to create a custom subclass and custom delegate methods for the UITabBar that --->replaces<--- the functionality of an -->actual<--- UITabBarController if you want to subclass your UITabBar.** However , as I said , **I see no reason for doing so**, unless you want to dig inside the UIKit and create your own UITabBar completely from scratch. Does that answer your question now? –  Mar 17 '17 at 15:41
  • The delegate methods provide no functionality for my question. However I will just assume that the answer to my question is -- it is not possible to replace the class of the tabBar within a tab bar controller programatically. Regardless of weather or not it is needed or not, that was my only question. – jontelang Mar 18 '17 at 07:18
  • Thanks for the replies either way, and sorry for the confusion as well. – jontelang Mar 18 '17 at 15:37
  • Np, go here and look through some examples you will see how to replace the tabbar with your custom one: https://www.cocoacontrols.com/search?q=tab+bar and here : https://www.cocoacontrols.com/search?q=uitabbar Good luck. –  Mar 18 '17 at 15:51

1 Answers1

1

I'm not sure about Objective-C but in Swift you can do this by overriding tabBar as a computed property:

open class MyTabBarController: UITabBarController {
    open override var tabBar: MyTabBar {
        return MyTabBar()
    }
}
Coli88
  • 302
  • 2
  • 9