更改UITabBar的色调/背景颜色
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
这些都是伟大的答案。如果允许自动旋转,将子视图的autoresizingMask设置为具有灵活的边距和大小会很有帮助,或者新的背景不会使用选项卡栏来调整大小。 – pmdj 2010-07-02 16:52:53