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

} 

왜 배경을 변경하는 두 가지 다른 방법이 있는지 이해가되지 않습니다. 이 두 코드 행은 다른 장소에서만 작동한다는 것은 혼란 스럽습니다. 나는 그것을 얻지 못한다. 하지만 이제는 효과가 있습니다.

관련 문제