首页 文章

改变UITabBar的色调/背景颜色

提问于
浏览
86

UINavigationBar和UISearchBar都有一个tintColor属性,允许你改变这两个项目的色调(我知道) . 我想在我的应用程序中对UITabBar做同样的事情,但现在已经找到了从默认的黑色中改变它的方法 . 有任何想法吗?

18 回答

  • 1

    对我来说,改变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"];
    

    试试这个!!!

  • 7
    [[UITabBar appearance] setTintColor:[UIColor redColor]];
     [[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];
    
  • 7

    [[self tabBar] insertSubview:v atIndex:0]; 对我来说非常合适 .

  • 0

    我已经能够通过子类化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
    
  • 0

    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;
    
  • 27

    我有一个最终答案的附录 . 虽然基本方案是正确的,但可以改进使用部分透明颜色的技巧 . 我假设它只是让默认渐变显示出来 . 哦,同样,TabBar的高度是49像素而不是48像素,至少在OS 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];
    
    }
    
  • 34

    当您使用addSubview时,您的按钮将失去可点击性,因此不会

    [[self tabBar] addSubview:v];
    

    使用:

    [[self tabBar] insertSubview:v atIndex:0];
    
  • 104

    没有简单的方法可以做到这一点,你基本上需要子类UITabBar并实现自定义绘图来做你想要的 . 对于这种效果来说,这是相当多的工作,但它可能是值得的 . 我建议向Apple提交一个错误,将其添加到未来的iPhone SDK中 .

  • 1

    以下是完美的解决方案 . 这适用于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];
    }
    
  • 3

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

    条形样式在视图内容和标签栏之间放置一个微妙的分隔符 .

  • 0

    for just background color

    Tabbarcontroller.tabBar.barTintColor=[UIColor redcolour];
    

    或者在App Delegate中

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

    for changing color of unselect icons of 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]];
    
  • 7

    现有答案中有一些好主意,许多工作略有不同,您选择的还取决于您定位的设备以及您希望实现的外观 . 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 个对象 . 如果您希望它们是平面颜色,我发现最简单的解决方案是使用所需的 backgroundColor 创建 UIView ,然后在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)最后,您可能想要自定义按钮 Headers 的样式 . 做:

    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
    [v setBackgroundColor ColorwithRed: Green: Blue: ];
    
  • 2

    另一个解决方案(这是一个hack)是将tabBarController上的alpha设置为0.01,这样它几乎不可见但仍然可以点击 . 然后使用alpha'ed tabBarCOntroller下面的自定义tabbar图像在MainWindow笔尖的底部设置一个ImageView控件 . 然后在tabbarcontroller切换视图时交换图像,更改颜色或高亮度 .

    但是,您失去了“...更多”和自定义功能 .

  • 48

    嗨我在使用iOS SDK 4,我只用两行代码解决了这个问题,就像这样

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

    希望这可以帮助!

  • 5
    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.0 answer: (来自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

    Swift 3使用 AppDelegate 的外观执行以下操作:

    UITabBar.appearance().barTintColor = your_color

相关问题