3

我试过这样做:

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

但它最终变成了黑色。起初我以为你不允许用纹理“着色”,但我最近看到了可以做到这一点的应用程序。我错过了什么吗?

谢谢。

4

2 回答 2

8

将 2 个文件添加到您的项目中,调用它们UINavigationBar-CustomTexture.hUINavigationBar-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]];
于 2010-03-26T06:10:13.307 回答
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];

于 2011-06-02T20:12:53.393 回答