2016-07-28 181 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

只是儘量附上屏幕截圖,問什麼你得到的是可以幫助讓這個問題更加清晰 –