Share background view between tabs on a UITabBarCo

2019-02-11 07:48发布

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.

2条回答
何必那么认真
2楼-- · 2019-02-11 08:25

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

查看更多
戒情不戒烟
3楼-- · 2019-02-11 08:45

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

查看更多
登录 后发表回答