MikeQ
MikeQ

Reputation: 1817

Share background view between tabs on a UITabBarController

Is it possible to have the same background between tabs on a UITabBarController without having to set the same background on all the views? I want to put a view in the background that periodically does a very short, non-resource intensive animation. When switching tabs, I'd like that animation to persist. I've read how to do it for UINavigationControllers, but haven't found any tips for a UITabBarController.

Upvotes: 1

Views: 5984

Answers (2)

james_womack
james_womack

Reputation: 10316

I've created a UITabBarController Additions category that allows you to do this. Keep in mind that the selected UIViewController will need to be transparent for you to see the background image.

//UITabBarController+CCAdditions.h

@interface UITabBarController (CCAdditions) 

- (void) setBackgroundImage:(UIImage *)i;

@end

//UITabBarController+CCAdditions.m

#import "UITabBarController+CCAdditions.h"

@implementation UITabBarController (CCAdditions)

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

@end

//example use in an apdelegate

#import "UITabBarController+CCAdditions.h"


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [tabBarController setBackgroundImage:[UIImage imageNamed:@"icon.png"]];
    [tabBarController.view setNeedsDisplay];
    return YES;
}

I use colorWithPatternImage instead of backgroundImage because it allows tiling when necessary

Upvotes: 5

thierryb
thierryb

Reputation: 3738

Create a Main View Controller which will have the background then from this controller add other views with transparent background, you could also put your background in your application window.

thierry

Upvotes: 0

Related Questions