2012-11-13 39 views
0

我可以设置背景一次,但在此之后它不会再次更改。我已经看到了stackoverflow上的所有例子。代码示例看起来总是一样的。我已经设置了委托。图像都可以。我已经将它们设置为默认图像,并显示。但是,应用程序完成启动后,背景中再也没有任何事情发生。当点击项目时TabBar上的setBackgroundImage不起作用

这里是我的代码:

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

我在寻找,也没有几个小时,想不通为什么它只有一次。有人在我的代码中看到错误吗?

回答

0

再过一小时后,我在网上的其他地方找到了解决方案(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]; 

} 

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

相关问题