2016-07-28 131 views
0

嗨,我是新手到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

我该怎么办呢?

回答

0

首先你应该记住的UITabBar的该配置应该在AppDelegate.m做到:

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

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

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"]]; 
+0

我已经在AppDelegate.m本身添加它 – remyr3my

+0

连这个只设置背景 – remyr3my

0

设置选择显示器的图像与外观工作对我来说直接的TabBar一起。

- (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; 
} 
+0

即使这样我得到同样的结果 – remyr3my

+0

只是尽量附上屏幕截图,问什么你得到的是可以帮助让这个问题更加清晰 –