Granit
Granit

Reputation: 1281

Custom UITabBarController using subclass

I want to create a custom UITabbarController by adding a subclass of UITabbarController to my project.

The custom tabbar contains custom icons for selected and unselected state as well as a tabs background which i want to add.

I added the the delegates to my .h file as follows:

#import <UIKit/UIKit.h>

@interface CustomTabViewController : UITabBarController <UITabBarDelegate,UITabBarControllerDelegate>
@property (strong, nonatomic) UIWindow *window;

@end

And in my implementation file in the viewDidLoad method i declared all my images which i want to use.

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITabBarController *tabController = [[UITabBarController alloc] init];

    UIImage *tabBackground = [[UIImage imageNamed:@"[email protected]"]
                              resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 320, 60)];
    [[UITabBar appearance] setBackgroundImage:tabBackground];

    self.window.rootViewController = tabController;

    UIImage *selectedImage = [UIImage imageNamed:@"[email protected]"];
    UIImage *unSelectedImage = [UIImage imageNamed:@"[email protected]"];
    UITabBar *tabBar = (UITabBar *)tabController.tabBar;
    UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
    [item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];

    selectedImage = [UIImage imageNamed:@"[email protected]"];
    unSelectedImage = [UIImage imageNamed:@"[email protected]"];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:1];
    [item2 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];

    selectedImage = [UIImage imageNamed:@"[email protected]"];
    unSelectedImage = [UIImage imageNamed:@"[email protected]"];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:2];
    [item3 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];

    selectedImage = [UIImage imageNamed:@"[email protected]"];
    unSelectedImage = [UIImage imageNamed:@"[email protected]"];
    UITabBarItem *item4 = [tabBar.items objectAtIndex:3];
    [item4 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage];

}

But when i run the app there are no changes to the default tab bar controller. I also set its' class to the custom class i created.

I wanted to ask if i am using the right approach here or are there any other methods i need to implement?

Thank you in advance!

Granit

Upvotes: 1

Views: 4689

Answers (1)

Nikos M.
Nikos M.

Reputation: 13783

You have to set also the frame for the tabBar like this:

CGRect smallFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width-300, frame.size.height-100);

tabController = [[UITabBarController alloc] init];

tabController.view.frame = smallFrame;

Upvotes: 1

Related Questions