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

Mik*_*keQ 1 iphone uitabbarcontroller uiview

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

jam*_*ack 5

我已经创建了一个UITabBarController Additions类,允许你这样做.请记住,所选的UIViewController需要透明才能看到背景图像.

//UITabBarController+CCAdditions.h

@interface UITabBarController (CCAdditions) 

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

@end
Run Code Online (Sandbox Code Playgroud)

//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
Run Code Online (Sandbox Code Playgroud)

//示例在apdelegate中使用

#import "UITabBarController+CCAdditions.h"


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

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