在UITabBarController上的选项卡之间共享背景视图 [英] Share background view between tabs on a UITabBarController

查看:107
本文介绍了在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

这篇关于在UITabBarController上的选项卡之间共享背景视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆