24

当我为 UITabBar 制作图标时,它会对图像应用渐变。我需要知道如何防止它出现这种渐变。

4

7 回答 7

27

苹果在 iOS 5 中添加了标签栏自定义,现在这种东西是微不足道的。在此之前,这是一个巨大的黑客攻击,不推荐。

以下是如何做一个完全自定义的标签栏:

// custom icons
UITabBarItem *item = [[UITabBarItem alloc] init];
item.title = @"foo";
// setting custom images prevents the OS from applying a tint color
[item setFinishedSelectedImage:[UIImage imageNamed:@"tab1_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab1_image_deselected.png"]];
tab1ViewController.tabBarItem = item;

    // tab bar

    // set background image - will be used instead of glossy black
    tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bar_bg.png"];
    // optionally set the tint color - setting this ti nil will result in the standard, blue tint color. tint color is ignored when custom icons are set as above.
    tabBarController.tabBar.selectedImageTintColor = nil;
    // remove the highlight around the selected tab - or provide an alternate highlight image. If you don't do this the iOS default is to draw a highlighted box beneath the selected tab icon.
    tabBarController.tabBar.selectionIndicatorImage = [[UIImage alloc] init];
于 2012-03-02T08:15:07.330 回答
18

这非常困难,因为UITabBar不提供对其选定/未选定图像的访问。不过,它可以通过私有 API 来实现:

@interface UITabBar (ColorExtensions)
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
@end

@interface UITabBarItem (Private)
@property(retain, nonatomic) UIImage *selectedImage;
- (void)_updateView;
@end

@implementation UITabBar (ColorExtensions)
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
{
    CGColorRef cgColor = [color CGColor];
    CGColorRef cgShadowColor = [shadowColor CGColor];
    for (UITabBarItem *item in [self items])
        if ([item respondsToSelector:@selector(selectedImage)] &&
            [item respondsToSelector:@selector(setSelectedImage:)] &&
            [item respondsToSelector:@selector(_updateView)])
        {
            CGRect contextRect;
            contextRect.origin.x = 0.0f;
            contextRect.origin.y = 0.0f;
            contextRect.size = [[item selectedImage] size];
            // Retrieve source image and begin image context
            UIImage *itemImage = [item image];
            CGSize itemImageSize = [itemImage size];
            CGPoint itemImagePosition; 
            itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
            itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
            UIGraphicsBeginImageContext(contextRect.size);
            CGContextRef c = UIGraphicsGetCurrentContext();
            // Setup shadow
            CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
            // Setup transparency layer and clip to mask
            CGContextBeginTransparencyLayer(c, NULL);
            CGContextScaleCTM(c, 1.0, -1.0);
            CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
            // Fill and end the transparency layer
            CGContextSetFillColorWithColor(c, cgColor);
            contextRect.size.height = -contextRect.size.height;
            CGContextFillRect(c, contextRect);
            CGContextEndTransparencyLayer(c);
            // Set selected image and end context
            [item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
            UIGraphicsEndImageContext();
            // Update the view
            [item _updateView];
        }
}
@end

甚至可以创建一些非常酷的效果:

红色标签栏
(来源:booleanmagic.com

苹果很有可能会拒绝这样做的申请。如果在未来的操作系统更新中删除了私有 API,
-[UITabBar recolorItemsWithColor:shadowColor:shadowOffset:shadowBlur:]它将什么都不做,而不是崩溃。

于 2009-08-31T09:04:54.263 回答
4

添加渐变很简单,添加以下代码行:


CGFloat components[8] = {0.0,0.4,1.0,0.2,0.0,0.6,1.0,1.0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
CGContextDrawLinearGradient(cxt, colorGradient,CGPointZero,CGPointMake(0,contextRect.size.height),0);

这将使您非常接近 Apple 在标签栏上所做的事情,但并不准确。要到达那里,只需在组件中再添加两个点/颜色,而不是NULLin CGGradientCreateWithColorComponents,使用类似{0,0.5,0.6,1.0}. 事实上,您只需要一种背景颜色和三个 alpha 点(颜色部分全为 1,因为您只需要在保留单一颜色配置文件的同时进行着色)。

如果不清楚,我会发布我的代码......干杯。

于 2010-02-17T09:05:27.407 回答
3

在 iDev 食谱网站上使用自定义实现的标签栏有一个解决方案。

http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar

于 2011-03-11T02:20:24.977 回答
3

已经快一年了,但这里是代码。将此作为类别添加到 UIImage 或转换为类范围。请记住,您传递的图像(self)必须是仅 alpha 图像(蒙版是根据图像的可见部分创建的)。



- (UIImage *) imageWithBackgroundColor:(UIColor *)bgColor 
                           shadeAlpha1:(CGFloat)alpha1 
                           shadeAlpha2:(CGFloat)alpha2
                           shadeAlpha3:(CGFloat)alpha3 
                           shadowColor:(UIColor *)shadowColor 
                          shadowOffset:(CGSize)shadowOffset 
                            shadowBlur:(CGFloat)shadowBlur { 

    UIImage *image = self;

    CGColorRef cgColor = [bgColor CGColor];
    CGColorRef cgShadowColor = [shadowColor CGColor];

    CGFloat components[16] = {1,1,1,alpha1,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3};
    CGFloat locations[4] = {0,0.5,0.6,1};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  

    CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)4);

    CGRect contextRect;
    contextRect.origin.x = 0.0f;
    contextRect.origin.y = 0.0f;
    contextRect.size = [image size];
    //contextRect.size = CGSizeMake([image size].width+5,[image size].height+5);  
    // Retrieve source image and begin image context
    UIImage *itemImage = image;
    CGSize itemImageSize = [itemImage size];
    CGPoint itemImagePosition; 
    itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
    itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
    UIGraphicsBeginImageContext(contextRect.size);
    CGContextRef c = UIGraphicsGetCurrentContext();
    // Setup shadow
    CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
    // Setup transparency layer and clip to mask
    CGContextBeginTransparencyLayer(c, NULL);
    CGContextScaleCTM(c, 1.0, -1.0);
    CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
    // Fill and end the transparency layer
    CGContextSetFillColorWithColor(c, cgColor);     
    contextRect.size.height = -contextRect.size.height;
    CGContextFillRect(c, contextRect);
    CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(contextRect.size.width*1.0/4.0,contextRect.size.height),0);
    CGContextEndTransparencyLayer(c);
    //CGPointMake(contextRect.size.width*3.0/4.0, 0)
    // Set selected image and end context
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(colorGradient);

    return resultImage;

}

例如,以下将产生与原生标签栏非常相似的效果:



UIImage *niceImage = [[UIImage imageNamed:@"image_name"] imageWithBackgroundColor:[UIColor colorWithRed:41.0/255.0 green:147.0/255.0 blue:239.0/255.0 alpha:1.0] 
                                                                      shadeAlpha1:0.6 
                                                                      shadeAlpha2:0.0 
                                                                      shadeAlpha3:0.4 
                                                                      shadowColor:[UIColor blackColor] 
                                                                     shadowOffset:CGSizeMake(0.0f, -1.0f)  
                                                                       shadowBlur:3.0]; 

于 2011-01-22T01:33:32.303 回答
1

使用以下图像(假设 tabBar 有 5 个选项卡,如下所示)

  • 在此处输入图像描述
  • 在此处输入图像描述
  • 在此处输入图像描述
  • 在此处输入图像描述
  • 在此处输入图像描述

使用“TabBar Application”模板创建一个新项目并放置以下代码。

AppDel.h 文件的内容。

#import <UIKit/UIKit.h>

@interface cTabBarAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UIImageView *imgV;

@end

AppDel.m 文件的内容。

#import "cTabBarAppDelegate.h"

@implementation cTabBarAppDelegate
@synthesize window=_window;
@synthesize tabBarController=_tabBarController;
@synthesize imgV = _imgV;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.tabBarController.delegate=self;
    self.imgV.frame=CGRectMake(0, 425, 320, 55);
    [self.tabBarController.view addSubview:self.imgV];
    self.tabBarController.selectedIndex=0;
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
    switch (index) {
        case 0:
            self.imgV.image=[UIImage imageNamed:@"tBar1.png"];
            break;
        case 1:
            self.imgV.image=[UIImage imageNamed:@"tBar2.png"];
            break;
        case 2:
            self.imgV.image=[UIImage imageNamed:@"tBar3.png"];
            break;
        case 3:
            self.imgV.image=[UIImage imageNamed:@"tBar4.png"];
            break;
        case 4:
            self.imgV.image=[UIImage imageNamed:@"tBar5.png"];
            break;
        default:
            break;
    }
    return YES;
}
于 2011-07-01T10:28:58.357 回答
1

我问了一个关于如何为按钮的“offstate”着色的问题。有人给了我一个解决方案,它也有去除渐变的好处。这是问题和他的答案:

Q: iphone - tabbar set imagetintcolor (offstate)

答:查看 UITabBarItem 文档的“管理完成和选定的图像”任务部分。

于 2012-06-02T15:08:24.913 回答