如何删除中间UITabBarItem的selectionIndicatorImage

时间:2014-01-24 04:55:36

标签: ios objective-c uitabbar uitabbaritem

我正在使用自定义UITabBarItem图像作为中间项,因此我必须为selectionIndicatorImage创建UIImage。 根据这个答案Same question as mine我已经制作了代码。

代码

UIStoryboard *iPhoneSB = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UITabBarController *tbc = [iPhoneSB instantiateInitialViewController];
self.window.rootViewController = tbc;
tbc.delegate = self;

tbc.selectedIndex = 2;

UITabBar *tb = tbc.tabBar;

NSArray *items = tb.items;
for (UITabBarItem *tbi in items) {
  UIImage *image = tbi.image;
  tbi.selectedImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  tbi.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[tbc.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selected-tabbar-bg.png"]];

委托方法:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

    if (tabBarController.selectedIndex==2) {
        [tabBarController.tabBar setSelectionIndicatorImage:nil];
    } else {
        [tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selected-tabbar-bg.png"]];
    }
}

这要归功于答案。但是有一些问题。在selectionIndicatorImage内部第一次触摸后UITabBarItem仍为零(确定除了中间)。

例如:

在应用程序启动时,选择了第三个UITabBar。触摸第一项 - selectionIndicatorImage工作正常(第一项被选中)。触摸第三项(特定项目) - 没有selectionIndicator(它很好)。但在那之后,如果我先触摸 - 例如,也没有selectionIndicator。 如果我在那之后触摸第二个就会出现。  哪里错了?提前致谢。

1 个答案:

答案 0 :(得分:2)

将日志放入委托方法中,并检查项目中是否将selectionIndicatorImage设置为nil。

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 

 if(tabBarController.tabBar.selectionIndicatorImage == nil)
     NSLog("Starting point delegate: Selection indicator image is nill");
 else 
     NSLog("Starting Point Of delegate: Selection indicator image is available");

 if (tabBarController.selectedIndex==2) {
     [tabBarController.tabBar setSelectionIndicatorImage:nil];
 } else {
     [tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selected-tabbar-bg.png"]];
 } 

 if(tabBarController.tabBar.selectionIndicatorImage == nil)
     NSLog("Ending point delegate: Selection indicator image is nill");
 else 
     NSLog("Ending Point Of delegate: Selection indicator image is available");
   }