改变UITabBar的色调/背景颜色

87

在IT技术中,UINavigationBar和UISearchBar都有一个tintColor属性,允许您更改这些项目的色调颜色(是的,很惊讶)。我希望在我的应用程序中对UITabBar执行相同的操作,但是发现无法将其从默认的黑色更改。有任何想法吗?


这些都是很好的答案。如果您允许自动旋转,将子视图的autoresizingMask设置为具有灵活的边距和大小会很有帮助,否则新的背景不会随选项卡栏一起调整大小。 - pmdj
18个回答

3

只为背景色

Tabbarcontroller.tabBar.barTintColor=[UIColor redcolour];

在AppDelegate中可以这样做。
[[UITabBar appearance] setBackgroundColor:[UIColor blackColor]];

更改选项卡栏未选中图标颜色

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]];

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

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)];
}

当你需要选择一个UIImage对象时,可以使用selectedunselected。如果你想要它们成为纯色,最简单的方法是创建一个具有所需backgroundColorUIView,然后借助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 设置为透明,并将你的文本添加到第二部分的 selectedunselected 图像中。

0

你好,我正在使用iOS SDK 4,只需两行代码即可解决此问题,代码如下:

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

希望这能有所帮助!

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];
}

0

如果你想在 Swift 3 中使用 AppDelegate 来设置外观,可以按照以下步骤:

UITabBar.appearance().barTintColor = your_color


0
另一个解决方案(这是一种hack)是将tabBarController的alpha设置为0.01,使其几乎不可见但仍然可点击。然后在MainWindow nib底部设置一个ImageView控件,其中包含您自定义的tabbar图像,位于alpha'ed tabBarCOntroller下面。然后,在tabbarcontroller切换视图时交换图像、更改颜色或高亮显示。但是,您会失去“...更多”和自定义功能。

0

Swift 3.0答案:(来自Vaibhav Gaikwad)

更改选项卡栏未选择图标的颜色:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接