2012-11-13 71 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]; 

} 

我不明白爲什麼有兩種不同的方式來改變背景。令人困惑的是,這兩種不同的代碼行只能在不同的地方工作。我不明白。但現在它起作用了。

相關問題