2

So I have two tabbar items in my tabbar and each of them has an image with a round corner, the rounded corner is at location where they meet, as you can see in the picture. I am trying to set the background image of the tabbar to be transparent instead of that black as you can see, but so far I keep bumping into some view that doesn't want to be transparent. Here is what I am using for the moment:

[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"transparent"]]];

I have also tried the next piece of code, but with no success.

[tabBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"transparent"]]];
for(UIView *v in tabBar.subviews)
{
    if(v.class == NSClassFromString(@"_UITabBarBackgroundView")||v.class == NSClassFromString(@"UITabBarButton"))
    {
        [v setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"transparent"]]];
    }
    for(UIView *vc in v.subviews)
    {
        [vc setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"transparent"]]];
    }
}

Any suggestions? How can I find that view and make it transparent?

tabbar too tabbar

Carmichael
  • 467
  • 1
  • 8
  • 21

1 Answers1

2

You can implement a category for this, extending UITabBarController like:

UITabBarController+TransparentBackground.h

@interface UITabBarController (TransparentBackground) 
- (void) setBackgroundImage:(UIImage *)image;
@end

UITabBarController+TransparentBackground.m

#import "UITabBarController+TransparentBackground.h"
@implementation UITabBarController (TransparentBackground)

- (void) setBackgroundImage:(UIImage *)image 
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    imageView.backgroundColor = [UIColor colorWithPatternImage:image];
    [self.view addSubview:imageView];
    [self.view sendSubviewToBack:imageView];
    [self.view setOpaque:NO];
    [self.view setBackgroundColor:[UIColor clearColor]];
}

@end

Add #import "UITabBarController+TransparentBackground.h" in your AppDelegate.m file.

In Your AppDelegate.m's didFinishLaunchingWithOptions method, Add the following two lines (at top):

[tabBarController setBackgroundImage:[UIImage imageNamed:@"CustomTabBarBackground.png"]];
[tabBarController.view setNeedsDisplay];

Hope you get it working, by doing as said.

For more info on this: REFRENCE

EDIT

Add Property to your AppDelegate.h by writing:

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

Also do @sythesize tabBarController; in AppDelegate.m file.

And Connect this IBOutlet property with your corresponding TabBarController in XIB

viral
  • 4,168
  • 5
  • 43
  • 68
  • If I paste that into the AppDelegate.m file, I get errors like : Unknown receiver 'tabBarController'; did you mean 'UITabBarController'? – Carmichael Apr 09 '13 at 13:07
  • I am not using a XIB, I have a storyboard. I don't think that I can connect the IBOutlet to the storyboard. – Carmichael Apr 09 '13 at 13:26
  • Well, I cannot connect the IBOutlet to the tab bar controller from the storyboard, it doesn't want to go that way. I tried that by drag&drop to the storyboard from the AppDelegate and viceversa, and right click on stroryboard the tab bar controller, choosing referencing layout but still I cannot connect it. – Carmichael Apr 09 '13 at 13:36
  • `UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];` `UIViewController* m1 = [sb instantiateViewControllerWithIdentifier:@"m1"];` `UIViewController* c1 = [sb instantiateViewControllerWithIdentifier:@"c1"];` `[self.tabBarController setBackgroundImage:[UIImage imageNamed:@"transparent"]]; [self.tabBarController.view setNeedsDisplay]; self.tabBarController.viewControllers = @[m1,c1]; [self.window makeKeyAndVisible];` Am I missing something? Sorry about the format of this comment. – Carmichael Apr 09 '13 at 14:10