改变UITabBar的色调/背景颜色
-
05-09-2019 - |
题
在UINavigationBar的和的UISearchBar都有一个tintColor属性,允许您更改色调颜色(奇怪,我知道)这两个项目的。我想要做的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
其他提示
的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;
我有一个编的最终答案。虽然基本方案是正确的,使用部分透明的颜色的特技可以在提高。我认为这只是为让默认梯度显示出来。哦也,使用TabBar的高度是49个像素,而不是48,至少在OS 3。
因此,如果你有一个梯度的适当1×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];
}
当你只需要使用addSubview您的按钮将失去可以点击的,所以不是
[[self tabBar] addSubview:v];
使用:
[[self tabBar] insertSubview:v atIndex:0];
有没有简单的方法来做到这一点,你基本上需要继承UITabBar和执行自定义绘制做你想做什么。这是相当多的影响的工作,但它可能是值得的。我建议您提交的bug与苹果把它加入到未来的iPhone SDK。
下面是此完美的解决方案。这正常工作与我的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];
}
在的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];
酒吧风格把您的视图的内容和您的标签栏之间的微妙隔板。
[[self tabBar] insertSubview:v atIndex:0];
完美的作品对我来说。
我其非常简单的改变的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"];
试试这个!!!
[[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]];
有一些好的想法,在现有的答案,许多工作稍有不同,哪些是你选择也将取决于您定位的设备上,并且你的目标是什么样的样子来实现。 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
是明确的和从部分添加你的文字到selected
和unselected
图像(2)。
[v setBackgroundColor ColorwithRed: Green: Blue: ];
另一种解决方案(其是劈)是对tabBarController阿尔法设置为0.01,使得它是几乎看不见,但仍然可点击。然后设置在主窗口笔尖的底部与alpha'ed tabBarCOntroller下定制的TabBar图像的ImageView的控制。然后,当开关tabbarcontroller意见交换图像,改变颜色或hightlight。
不过,你输了“......更多”和自定义功能。
您好我使用的是iOS SDK 4,我能解决只用两行代码这个问题,它是这样的
tBar.backgroundColor = [UIColor clearColor];
tBar.backgroundImage = [UIImage imageNamed:@"your-png-image.png"];
希望这有助于!
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];
}
<强>夫特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)
使用从AppDelegate
外观夫特3执行以下操作:
UITabBar.appearance().barTintColor = your_color