setBackgroundImage on TabBar does not work when tapping items

*爱你&永不变心* 提交于 2019-12-11 14:04:36

问题


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.

Here's my code:

- (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];

}

The debugger shows:

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?


回答1:


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.



来源:https://stackoverflow.com/questions/13354504/setbackgroundimage-on-tabbar-does-not-work-when-tapping-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!