我试过这样做:
[toolbar setTint:[UIColor colorWithPatternImage:[UIImage imageNamed:@"thingFromMyBundle.png"]]];
但它刚刚变黑了。起初我假设你不允许用纹理“着色”,但我最近看到的应用程序可以做到这一点。我错过了什么吗?
感谢。
答案 0 :(得分:8)
将2个文件添加到您的项目中,例如,将它们称为UINavigationBar-CustomTexture.h
和UINavigationBar-CustomTexture.m
,在这些文件中输入:
.h文件:
#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomTexture)
@end
.m文件:
#import "UINavigationBar-CustomTexture.h"
@interface UIView (CustomTexture)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;
@end
@implementation UINavigationBar (CustomTexture)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
if ([self isMemberOfClass:[UINavigationBar class]]){
UIImage *image = [UIImage imageNamed:@"myImage.png"];
CGContextScaleCTM(ctx, 1.0f, -1.0f);
CGContextDrawImage(ctx, CGRectMake(0, -image.size.height, self.frame.size.width, self.frame.size.height), image.CGImage);
}else{
[super drawLayer:layer inContext:ctx];
}
}
@end
在实例化导航控制器的地方包含.h文件。 将您的png文件设置为320x44。根据您的纹理,将导航栏的色调颜色更改为这样(使导航栏上的按钮看起来更好):
[self.navigationController.navigationBar setTintColor:[UIColor colorWithHue:0 saturation:0 brightness:0.5f alpha:0.1f]];
答案 1 :(得分:2)
以下是我解决这个问题的方法:
_titleToolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth, 34.0f)];
[_titleToolbar setTranslucent:YES];
[_titleToolbar setBackgroundColor:[UIColor clearColor]];
CALayer* backgroundLayer = [CALayer layer];
[backgroundLayer setFrame:CGRectMake(0, 0, kDeviceWidth, _titleToolbar.frame.size.height)];
UIImage* patternImage = [UIImage deviceImageNamed:@"topToolbarBackground"];
[backgroundLayer setBackgroundColor:[UIColor colorWithPatternImage:patternImage].CGColor];
[[_titleToolbar layer] insertSublayer:backgroundLayer atIndex:0];
_titleToolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth, 34.0f)];
[_titleToolbar setTranslucent:YES];
[_titleToolbar setBackgroundColor:[UIColor clearColor]];
CALayer* backgroundLayer = [CALayer layer];
[backgroundLayer setFrame:CGRectMake(0, 0, kDeviceWidth, _titleToolbar.frame.size.height)];
UIImage* patternImage = [UIImage deviceImageNamed:@"topToolbarBackground"];
[backgroundLayer setBackgroundColor:[UIColor colorWithPatternImage:patternImage].CGColor];
[[_titleToolbar layer] insertSublayer:backgroundLayer atIndex:0];