在UITabBarController上的选项卡之间共享背景视图

问题描述:

是否可以在UITabBarController上的选项卡之间具有相同的背景而无需在所有视图上设置相同的背景?我想在后台放置一个视图,定期执行非常短的非资源密集型动画。切换标签时,我希望该动画能够持续存在。我已经阅读了如何为UINavigationControllers做到这一点,但是没有找到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.

我已经创建了一个允许您执行此操作的UITabBarController Additions类别。请记住,所选的UIViewController需要透明才能看到背景图像。

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

//UITabBarController+CCAdditions.h

@interface UITabBarController (CCAdditions) 

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

@end

// UITabBarController + CCAdditions.m

//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

//示例用于apdelegate

//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;
}

我使用colorWithPatternImage而不是backgroundImage,因为它允许在必要时进行平铺

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