点击项目时,TabBar 上的 setBackgroundImage 不起作用 [英] setBackgroundImage on TabBar does not work when tapping items

查看:23
本文介绍了点击项目时,TabBar 上的 setBackgroundImage 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以设置一次背景,但之后就再也不会改变了.我已经看过所有关于 stackoverflow 的例子.代码示例看起来总是一样的.我已经设置了代表.图片一切正常.我已经将它们一个接一个地设置为默认图像并显示出来.但是在应用程序完成启动后,后台不再发生任何事情.

I can set the background once, but after that it never changes again. I've seen all examples on stackoverflow. The code examples look always the same. I've set the delegate. The images are all ok. I've set them one after the other as default image and the will show. But after the app finished launching nothing happens with the background anymore.

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self customizeInterface];

    // Override point for customization after application launch.
    self.tabController = (UITabBarController *)self.window.rootViewController;
    self.tabController.delegate = self;
...
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    int tabitem = self.tabController.selectedIndex;
    NSLog(@"tabitem: %i", tabitem);
    [self switchTabBarImage:tabitem];
    //[[tabController objectAtIndex:tabitem] popToRootViewControllerAnimated:YES];
}

- (void)switchTabBarImage:(int)selectedIndex
{
    NSLog(@"selected: %i", selectedIndex);
    if (selectedIndex == 0) {
        NSLog(@"0");
        UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-1.png"];
        [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    }
    if (selectedIndex == 1) {
        NSLog(@"1");
        UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-2.png"];
        [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    }
    if (selectedIndex == 2) {
        NSLog(@"2");
        UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-3.png"];
        [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    }
    if (selectedIndex == 3) {
        NSLog(@"3");
        UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-4.png"];
        [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    }
}

- (void)customizeInterface
{

    UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-1.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];

    UIImage *selectionIndicator = [UIImage imageNamed:@"tabbar-icon-clean.png"];
    [[UITabBar appearance] setSelectionIndicatorImage:selectionIndicator];

}

调试器显示:

2012-11-13 02:42:06.147 soundapp[9060:c07] tabitem: 1
2012-11-13 02:42:06.148 soundapp[9060:c07] selected: 1
2012-11-13 02:42:06.148 soundapp[9060:c07] 1
2012-11-13 02:42:07.739 soundapp[9060:c07] tabitem: 2
2012-11-13 02:42:07.739 soundapp[9060:c07] selected: 2
2012-11-13 02:42:07.740 soundapp[9060:c07] 2

我搜索了几个小时,但不明白为什么它只能工作一次.有人发现我的代码有错误吗?

I'm searching nor for hours and can't figure out why it works only once. Does anybody see a mistake in my code?

推荐答案

又过了一个小时,我在网上的其他地方找到了解决方案 (http://felipecypriano.com/2012/02/27/how-to-自定义uitabbar-on-ios-5/)

After another hour i've found the solution somewhere else on the web (http://felipecypriano.com/2012/02/27/how-to-customize-uitabbar-on-ios-5/)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    int tabitem = self.tabController.selectedIndex;
    NSLog(@"tabitem: %i", tabitem);
    [self switchTabBarImage:tabitem];
    //[[tabController objectAtIndex:tabitem] popToRootViewControllerAnimated:YES];
}

- (void)switchTabBarImage:(int)selectedIndex
{

    switch (selectedIndex) {
        case 0:
            [[[self tabController] tabBar] setBackgroundImage:[UIImage imageNamed:@"tabbar-1.png"]];
            break;
        case 1:
            [[[self tabController] tabBar] setBackgroundImage:[UIImage imageNamed:@"tabbar-2.png"]];
            break;
        case 2:
            [[[self tabController] tabBar] setBackgroundImage:[UIImage imageNamed:@"tabbar-3.png"]];
            break;
        case 3:
            [[[self tabController] tabBar] setBackgroundImage:[UIImage imageNamed:@"tabbar-4.png"]];
            break;
    }
}

- (void)customizeInterface
{

    UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar-1.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];

    UIImage *selectionIndicator = [UIImage imageNamed:@"tabbar-icon-clean.png"];
    [[UITabBar appearance] setSelectionIndicatorImage:selectionIndicator];

}

我真的不明白为什么有两种不同的方式来改变背景.令人困惑的是,这两条不同的代码行只在不同的地方工作.我不明白.但现在它起作用了.

I don't really understand why there are two different ways to change the background. It's confusing that these two different code lines work only in different places. I don't get that. But now it works.

这篇关于点击项目时,TabBar 上的 setBackgroundImage 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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