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

时间:2010-04-15 13:31:58

标签: iphone uiview uitabbarcontroller

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

2 个答案:

答案 0 :(得分:5)

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

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

//在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,因为它允许在必要时进行平铺

答案 1 :(得分:0)

创建一个主视图控制器,它将具有背景,然后从该控制器添加具有透明背景的其他视图,您还可以将您的背景放在应用程序窗口中。

亨利

相关问题