故事板中tabbar上的自定义背景

时间:2012-08-01 04:15:53

标签: iphone ios storyboard tabbar ios5

所以我不得不承认,我已经完成了这段代码直到大约一天前我添加了一个新视图,所以我现在非常沮丧。

设置: 我有一个包含标签栏的故事板应用程序。在AppDelegate中,我有以下内容用于附加CoreData

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navigationController = [[tabBarController viewControllers] objectAtIndex:0];
    GamesViewController *controller = [[navigationController viewControllers] objectAtIndex:0];
    controller.managedObjectContext = self.managedObjectContext;

    return YES;
}

同样在AppDelegate中,我有这个方法将标准选项卡背景设置为我选择的图像:

- (void)customizeInterface {    
    UIImage* tabBarBackground = [UIImage imageNamed:@"tab_background"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
}

所以这一切都很好直到我添加了另一个登录视图,它位于我的标签之前。我不得不改变CoreData最初设置的内容(从我的标签到我的登录/初始化视图)。以下是故事板外观的新设置。

Here's the new setup in my storyboard

现在,当应用加载时...背景图像最初与之前一样显示,但第一个标签上只显示 。一旦我点击它,它再次切换到默认渐变颜色。如果我返回第一个/初始选项卡,背景不会重新应用,它将保持为彩色渐变。

这是修改后的applicationDidFinishLaunching代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //instantiate local context
    NSManagedObjectContext *context = [self managedObjectContext];
    if (!context) {
        // Handle the error.
        NSLog(@"Error: Context is null");
    }

    LoginViewController *rootViewController = [LoginViewController alloc];
    rootViewController.managedObjectContext = context;

    return YES;
}

那么我尝试做的是进入我的tabbar加载的第一个VC中的viewDidLoad(GameViewController)并尝试添加它以解决问题:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"dock_background"]];
}

这不起作用,所以我也尝试使用我在AppDelegate中使用的相同原始代码,但也没有用:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage* tabBarBackground = [UIImage imageNamed:@"tab_background"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
}

所以...我有点卡住了。我必须做(或不做)一些如此明显的事情......那里有人有任何提示/指示吗?

非常感谢 - 德鲁

1 个答案:

答案 0 :(得分:0)

Delegate.h文件

@interface AppDelegate:UIResponder

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIImageView *imgV;
@property (strong, nonatomic) UIViewController *viewController;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

Delegate.m文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

        self.viewController = [[DeskboardVctr alloc] initWithNibName:@"DeskboardVctr" bundle:nil];




    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate=self;
    self.imgV=[[UIImageView alloc] init];
    self.imgV.frame=CGRectMake(0, 0, 1024, 49);
    [[self.tabBarController tabBar] insertSubview:self.imgV atIndex:1];
    self.tabBarController.delegate=self;
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

Tabbar委派方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
        switch (index) {
            case 0:
                self.imgV.image=[UIImage imageNamed:@"t1.png"];
                break;
            case 1:
                self.imgV.image=[UIImage imageNamed:@"t2.png"];
                break;
            case 2:
                self.imgV.image=[UIImage imageNamed:@"t3.png"];
                break;
            case 3:
                self.imgV.image=[UIImage imageNamed:@"t4.png"];
                break;
            case 4:
                self.imgV.image=[UIImage imageNamed:@"t5.png"];
                break;
            default:
                break;
        }
    return YES;
}

希望你能帮忙解决这个问题 感谢