views:

396

answers:

1

I tried doing this:

[toolbar setTint:[UIColor colorWithPatternImage:[UIImage imageNamed:@"thingFromMyBundle.png"]]];

but it just ended up black. At first I assumed you weren't allowed to "tint" with a texture, but I've seen apps recently that can do this. Am I missing something?

Thanks.

+5  A: 

Add 2 files to your project, call them UINavigationBar-CustomTexture.h and UINavigationBar-CustomTexture.m for example, in those files put this:

.h file:

#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomTexture)
@end

.m file:

#import "UINavigationBar-CustomTexture.h"
@implementation UINavigationBar (CustomTexture)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    if ([self isMemberOfClass:[UINavigationBar class]]){
        UIImage *image = [UIImage imageNamed:@"thingFromMyBundle.png"];
        CGContextClip(ctx);
        CGContextTranslateCTM(ctx, 0, image.size.height);
        CGContextScaleCTM(ctx, 1.0f, -1.0f);
        CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
    }else{
        [super drawLayer:layer inContext:ctx];
    }
}
@end

Include the .h file where you instantiate your navigation controller. Make your png file 320x44. Depending on your texture, change the tint color of your navigation bar to something like this (to make the buttons on the navigation bar look better):

[self.navigationController.navigationBar setTintColor:[UIColor colorWithHue:0 saturation:0 brightness:0.5f alpha:0.1f]];
Zoran Simic
It works for me, but I'm getting an "<Error>: doClip: empty path.".If I remove the CGContextClip(ctx); them the warning goes away (and everything still works).
morais