选项卡栏的选择指示灯不起作用

时间:2016-07-28 09:00:15

标签: ios objective-c uitabbarcontroller

嗨,我是iOS的新手。

我已经通过Storyboard实现了tabBarController以及4个tabBar项目。现在,我需要自定义我的标签栏,如下图所示。我已经设置了标签栏的背景。

 + (UIImage *)imageFromColor:(UIColor *)color {
     CGRect rect = CGRectMake(0, 0, 1, 1);
     UIGraphicsBeginImageContext(rect.size);
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextSetFillColorWithColor(context, [color CGColor]);
     CGContextFillRect(context, rect);
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return image;
 }


 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

      [[UITabBar appearance] setTintColor:[UIColor whiteColor]];

      [[UITabBar appearance] setBackgroundImage:[AppDelegate imageFromColor:[UIColor blackColor]]];

      return YES;
   }

标签栏的背景设置没有任何问题。

当我尝试为所选标签栏项目设置颜色时,它不起作用。我不知道为什么?

[[UITabBar appearance]setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor orangeColor]]];

我需要像这样自定义标签栏:

enter image description here

我该怎么做?

2 个答案:

答案 0 :(得分:0)

首先你应该记住,UITabBar的配置应该在AppDelegate.m中完成:

您应该添加[[UITabBar appearance]setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor orangeColor]]];  AppDelegate.m方法中的applicationdidFinishLaunchingWithOptions

其次在你的情况下你应该提供标签栏项目选择状态示例:

   UITabBarController *tabBarController = (UITabBarController   *)self.window.rootViewController;
   UITabBar *tabBar = tabBarController.tabBar;
   UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];

   //We set title
   tabBarItem1.title = @"Home";

   //We set highlighted state
   [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]];

   // Change the tab bar background
   UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
   [[UITabBar appearance] setBackgroundImage:tabBarBackground];
   // Tab bar active background
   [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];

答案 1 :(得分:0)

将选择指示器图像直接设置到标签栏以及外观适用于我。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

     UITabBarController *tabBarContr = (UITabBarController *)self.window.rootViewController;
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_bar_selection_indicator.png"]];

    // iOS7 hack: to make selectionIndicatorImage appear on the selected tab on the first app run
    [[tabBarContr tabBar] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_bar_selection_indicator.png"]];

    return YES;
}