Предотвращение применения UITabBar градиента к изображениям своих значков - PullRequest
24 голосов
/ 31 августа 2009

Когда я делаю иконки для UITabBar, он применяет градиент к изображениям. Мне нужно знать, как избежать этого градиента.

Ответы [ 7 ]

27 голосов
/ 02 марта 2012

Apple добавила настройку панели вкладок в 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];
18 голосов
/ 31 августа 2009

Это удивительно сложно, так как 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

Можно даже создать несколько довольно крутых эффектов:

Red Tab Bar
(источник: booleanmagic.com )

Вполне возможно, что Apple отклонит заявку на это. Если частный API будет удален в будущем обновлении ОС,
-[UITabBar recolorItemsWithColor:shadowColor:shadowOffset:shadowBlur:] ничего не сделает вместо сбоя.

4 голосов
/ 17 февраля 2010

Добавить градиент очень просто, добавьте следующие строки кода:


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 делает на панели вкладок, но не точно. Для этого просто добавьте еще две точки / цвета в компоненты и вместо NULL в CGGradientCreateWithColorComponents используйте что-то вроде {0,0.5,0.6,1.0}. На самом деле все, что вам нужно, это один фоновый цвет и три альфа-точки (с цветовой частью, равной всем 1, поскольку вам просто нужно затенение при сохранении одного цветового профиля).

Я опубликую свой код, если неясно ... ура.

3 голосов
/ 11 марта 2011

Существует решение этой проблемы с использованием настраиваемой панели вкладок на сайте iDev Recipes.

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

3 голосов
/ 22 января 2011

Прошел почти год, но вот код. Добавьте это как категорию в UIImage или конвертируйте в класс. Помните, что передаваемое вами (само) изображение должно быть альфа-изображением (маска создается на основе видимых частей изображения).



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

1 голос
/ 02 июня 2012

Я задал вопрос о том, как раскрасить «offstate» кнопки. Кто-то дал мне решение, у которого также был бонус удаления градиента. вот вопрос и его ответ:

Q: iphone - набор вкладок imagetintcolor (offstate)

A: Ознакомьтесь с разделом задачи «Управление готовым и выбранным изображением» документов UITabBarItem.

1 голос
/ 01 июля 2011

Использовать следующие изображения (при условии, что tabBar имеет 5 вкладок следующим образом)

  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here

Создайте новый проект с помощью шаблона «Приложение TabBar» и разместите следующий код.

Содержимое файла 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;
}
...