2009-02-20 322 views
84

UINavigationBar和UISearchBar都有一個tintColor屬性,允許您更改這兩個項目的色調(令人驚訝,我知道)。我想在我的應用程序中對UITabBar執行相同的操作,但現在已經找到了將其從默認黑色更改爲其它方法的方法。有任何想法嗎?更改UITabBar的色調/背景顏色

+0

這些都是偉大的答案。如果允許自動旋轉,將子視圖的autoresizingMask設置爲具有靈活的邊距和大小會很有幫助,或者新的背景不會使用選項卡欄來調整大小。 – pmdj 2010-07-02 16:52:53

回答

48

我已經能夠使其通過繼承一個的UITabBarController和使用私有類工作:

@interface UITabBarController (private) 
- (UITabBar *)tabBar; 
@end 

@implementation CustomUITabBarController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

    CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48); 
    UIView *v = [[UIView alloc] initWithFrame:frame]; 
    [v setBackgroundColor:kMainColor]; 
    [v setAlpha:0.5]; 
    [[self tabBar] addSubview:v]; 
    [v release]; 

} 
@end 
+1

這是一種奇怪的解決方案,它只是在標籤欄頂部放置半透明的棕色矩形。問題是所有的按鈕都變成棕色,而不僅僅是背景。不過,這似乎是迄今爲止所有人提出的最佳選擇。 – Jonah 2009-08-18 20:04:56

+0

這是一個私人API?如果我在我的應用程序中使用這個,我會被拒絕嗎? – Frank 2010-11-16 18:23:38

+0

似乎沒有任何私人電話在那裏沒有 – 2010-11-25 18:02:11

6

有沒有簡單的方法來做到這一點,你基本上需要子類UITabBar和實現自定義繪圖做你想做的。對於這個效果來說這是一個相當大的工作,但它可能是值得的。我建議向Apple提交一個錯誤,以便將它添加到未來的iPhone SDK中。

+4

先生,你是否填補了蘋果的錯誤? – 2009-10-30 18:37:58

1
[v setBackgroundColor ColorwithRed: Green: Blue: ]; 
27

當你只需要使用addSubview您的按鈕將失去可以點擊的,所以不是

[[self tabBar] addSubview:v]; 

使用:

[[self tabBar] insertSubview:v atIndex:0]; 
34

我有一個最終答案的附錄。儘管基本方案是正確的,但使用部分透明顏色的技巧可以得到改進。我假設它只是讓默認漸變顯示。哦,同樣,TabBar的高度是49像素,而不是48,至少在操作系統3中。

所以,如果你有一個適當的1 x 49圖像的梯度,這是你應該使用的viewDidLoad版本:完美的我

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    CGRect frame = CGRectMake(0, 0, 480, 49); 
    UIView *v = [[UIView alloc] initWithFrame:frame]; 
    UIImage *i = [UIImage imageNamed:@"GO-21-TabBarColorx49.png"]; 
    UIColor *c = [[UIColor alloc] initWithPatternImage:i]; 
    v.backgroundColor = c; 
    [c release]; 
    [[self tabBar] addSubview:v]; 
    [v release]; 

} 
5

[[self tabBar] insertSubview:v atIndex:0]; 作品。

0

另一個解決方案(這是一個黑客)是將tabBarController上的alpha設置爲0.01,以便它幾乎不可見但仍然可點擊。然後使用alpha'ed tabBarCOntroller下的自定義tabbar圖像在MainWindow nib底部設置一個ImageView控件。然後交換圖像,當tabbarcontroller切換視圖時更改顏色或高光。

但是,您失去了'...更多'和自定義功能。

103

iOS 5增加了一些新的外觀方法來定製大多數UI元素的外觀。

您可以通過使用外觀代理來爲應用程序中的每個UITabBar實例定位。

對於iOS 5 + 6:

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

對於iOS 7及以上,請使用以下:

[[UITabBar appearance] setBarTintColor:[UIColor redColor]]; 

使用外觀代理將改變整個應用程序的任何選項卡欄實例。對於一個具體的實例,使用這個類的新特性之一:

UIColor *tintColor; // iOS 5+6 
UIColor *barTintColor; // iOS 7+ 
UIColor *selectedImageTintColor; 
UIImage *backgroundImage; 
UIImage *selectionIndicatorImage; 
0

您好我使用的是iOS SDK 4,我能解決只用兩行代碼這個問題,它是這樣的

tBar.backgroundColor = [UIColor clearColor]; 
tBar.backgroundImage = [UIImage imageNamed:@"your-png-image.png"]; 

希望這有助於!

7

以下是完美的解決方案。這適用於iOS5和iOS4。

//---- For providing background image to tabbar 
UITabBar *tabBar = [tabBarController tabBar]; 

if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) { 
    // ios 5 code here 
    [tabBar setBackgroundImage:[UIImage imageNamed:@"image.png"]]; 
} 
else { 
    // ios 4 code here 
    CGRect frame = CGRectMake(0, 0, 480, 49); 
    UIView *tabbg_view = [[UIView alloc] initWithFrame:frame]; 
    UIImage *tabbag_image = [UIImage imageNamed:@"image.png"]; 
    UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image]; 
    tabbg_view.backgroundColor = tabbg_color; 
    [tabBar insertSubview:tabbg_view atIndex:0]; 
} 
1

有一些好的想法,在現有的答案,許多工作稍有不同,哪些是你選擇也將取決於您定位的設備上,並且你的目標是什麼樣的樣子來實現。 UITabBar當它來定製它的外觀時是非常不直觀的,但是這裏有幾個技巧可以幫助:

1)。如果你想擺脫光澤覆蓋更扁平的外觀做:

tabBar.backgroundColor = [UIColor darkGrayColor]; // this will be your background 
[tabBar.subviews[0] removeFromSuperview]; // this gets rid of gloss 

2)。要設置自定義圖像的TabBar按鈕做這樣的事情:

for (UITabBarItem *item in tabBar.items){ 
    [item setFinishedSelectedImage:selected withFinishedUnselectedImage:unselected]; 
    [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)]; 
} 

selectedunselected是您選擇的UIImage對象。如果你希望它們是平坦的顏色,我發現最簡單的解決方案是創建一個UIView與所需的backgroundColor,然後在QuartzCore的幫助下將其渲染爲UIImage。我用下面的方法在一個類別上UIView得到的觀點的內容UIImage

- (UIImage *)getImage { 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen]scale]); 
    [[self layer] renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return viewImage; 
} 

3)最後,你可能想自定義按鈕頭銜的造型。做:

for (UITabBarItem *item in tabBar.items){ 
    [item setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: 
       [UIColor redColor], UITextAttributeTextColor, 
       [UIColor whiteColor], UITextAttributeTextShadowColor, 
       [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
       [UIFont boldSystemFontOfSize:18], UITextAttributeFont, 
      nil] forState:UIControlStateNormal]; 
} 

這讓你做一些調整,但仍然相當有限。特別是,您不能自由修改文本放置在按鈕內的位置,並且對於選定/未選定的按鈕不能有不同的顏色。如果您想要進行更具體的文字佈局,只需將UITextAttributeTextColor設置爲清晰,並將文本添加到第(2)部分的selectedunselected圖像中即可。

5

對我來說它很簡單,改變的TabBar的顏色,如: -

[self.TabBarController.tabBar setTintColor:[UIColor colorWithRed:0.1294 green:0.5686 blue:0.8353 alpha:1.0]]; 


[self.TabBarController.tabBar setTintColor:[UIColor "YOUR COLOR"]; 

試試這個!

0
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) { 
    // ios 5 code here 
    [tabBar setBackgroundImage:[UIImage imageNamed:@"image.png"]]; 
} 
else { 
    // ios 4 code here 
    CGRect frame = CGRectMake(0, 0, 480, 49); 
    UIView *tabbg_view = [[UIView alloc] initWithFrame:frame]; 
    UIImage *tabbag_image = [UIImage imageNamed:@"image.png"]; 
    UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image]; 
    tabbg_view.backgroundColor = tabbg_color; 
    [tabBar insertSubview:tabbg_view atIndex:0]; 
} 
7

在iOS上7:

[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:(38.0/255.0) green:(38.0/255.0) blue:(38.0/255.0) alpha:1.0]]; 

我還建議設置首先取決於你的視覺慾望:

[[UITabBar appearance] setBarStyle:UIBarStyleBlack]; 

酒吧風格把你的觀點的內容和你的標籤之間的微妙分離器酒吧。

3
[[UITabBar appearance] setTintColor:[UIColor redColor]]; 
[[UITabBar appearance] setBarTintColor:[UIColor yellowColor]]; 
2

只是背景顏色

Tabbarcontroller.tabBar.barTintColor=[UIColor redcolour]; 

或這在應用代表

[[UITabBar appearance] setBackgroundColor:[UIColor blackColor]]; 

用於改變的TabBar的非選擇圖標顏色

對於iOS 10:

// this code need to be placed on home page of tabbar  
for(UITabBarItem *item in self.tabBarController.tabBar.items) { 
    item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 
} 

以上的iOS 10:

// this need to be in appdelegate didFinishLaunchingWithOptions 
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]]; 
0

夫特3.0答案:(從Vaibhav的Gaikwad)

對於改變的TabBar的非選擇圖標顏色:

if #available(iOS 10.0, *) { 
     UITabBar.appearance().unselectedItemTintColor = UIColor.white 
    } else { 
     // Fallback on earlier versions 
     for item in self.tabBar.items! { 
      item.image = item.image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     } 
} 

對於僅更改文字顏色:

使用
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal) 

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red, for: .selected) 
0

斯威夫特3的外觀從AppDelegate做到以下幾點:

UITabBar.appearance().barTintColor = your_color