UINavigationBar和UISearchBar都有一個tintColor屬性,允許您更改這兩個項目的色調(令人驚訝,我知道)。我想在我的應用程序中對UITabBar執行相同的操作,但現在已經找到了將其從默認黑色更改爲其它方法的方法。有任何想法嗎?更改UITabBar的色調/背景顏色
回答
我已經能夠使其通過繼承一個的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
有沒有簡單的方法來做到這一點,你基本上需要子類UITabBar和實現自定義繪圖做你想做的。對於這個效果來說這是一個相當大的工作,但它可能是值得的。我建議向Apple提交一個錯誤,以便將它添加到未來的iPhone SDK中。
先生,你是否填補了蘋果的錯誤? – 2009-10-30 18:37:58
[v setBackgroundColor ColorwithRed: Green: Blue: ];
當你只需要使用addSubview您的按鈕將失去可以點擊的,所以不是
[[self tabBar] addSubview:v];
使用:
[[self tabBar] insertSubview:v atIndex:0];
我有一個最終答案的附錄。儘管基本方案是正確的,但使用部分透明顏色的技巧可以得到改進。我假設它只是讓默認漸變顯示。哦,同樣,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];
}
[[self tabBar] insertSubview:v atIndex:0];
作品。
另一個解決方案(這是一個黑客)是將tabBarController上的alpha設置爲0.01,以便它幾乎不可見但仍然可點擊。然後使用alpha'ed tabBarCOntroller下的自定義tabbar圖像在MainWindow nib底部設置一個ImageView控件。然後交換圖像,當tabbarcontroller切換視圖時更改顏色或高光。
但是,您失去了'...更多'和自定義功能。
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;
您好我使用的是iOS SDK 4,我能解決只用兩行代碼這個問題,它是這樣的
tBar.backgroundColor = [UIColor clearColor];
tBar.backgroundImage = [UIImage imageNamed:@"your-png-image.png"];
希望這有助於!
以下是完美的解決方案。這適用於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];
}
有一些好的想法,在現有的答案,許多工作稍有不同,哪些是你選擇也將取決於您定位的設備上,並且你的目標是什麼樣的樣子來實現。 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)];
}
凡selected
和unselected
是您選擇的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)部分的selected
和unselected
圖像中即可。
對我來說它很簡單,改變的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"];
試試這個!
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];
}
在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];
酒吧風格把你的觀點的內容和你的標籤之間的微妙分離器酒吧。
[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];
只是背景顏色
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]];
夫特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)
斯威夫特3的外觀從AppDelegate
做到以下幾點:
UITabBar.appearance().barTintColor = your_color
- 1. 變化uitabbar背景顏色
- 2. 如何將UITabbar所選標籤顏色的背景顏色更改爲藍色?
- 3. 更改背景顏色3
- 4. jqueryMobile更改背景顏色
- 5. 更改背景顏色tabhost
- 6. 更改UIButton背景顏色
- 7. 更改背景顏色8
- 8. WL.SimpleDialog更改背景顏色
- 9. 更改背景顏色
- 10. UICollectionViewCell更改背景顏色
- 11. 更改QLPreviewController背景顏色
- 12. 更改背景顏色
- 13. 更改UIAlertcontroller背景顏色
- 14. 更改TToolBar背景顏色
- 15. Gnuplot - 更改背景顏色
- 16. select2更改背景顏色
- 17. 更改fancybox2背景顏色
- 18. 更改背景顏色
- 19. 更改DIV背景顏色
- 20. 更改背景顏色
- 21. 更改Listitem背景顏色
- 22. QMenu更改背景顏色
- 23. 更改背景顏色
- 24. 更改HighCharts背景顏色?
- 25. 更改背景顏色
- 26. model.addrow()更改背景顏色
- 27. 更改背景顏色
- 28. 更改UITableViewController背景顏色
- 29. 更改背景顏色
- 30. Bootstrap:更改背景顏色
這些都是偉大的答案。如果允許自動旋轉,將子視圖的autoresizingMask設置爲具有靈活的邊距和大小會很有幫助,或者新的背景不會使用選項卡欄來調整大小。 – pmdj 2010-07-02 16:52:53