我可以将纹理应用于UIToolbar吗?

Ben*_*tto 3 iphone textures colors uitoolbar

我试过这样做:

[toolbar setTint:[UIColor colorWithPatternImage:[UIImage imageNamed:@"thingFromMyBundle.png"]]];
Run Code Online (Sandbox Code Playgroud)

但它只是黑色.起初我假设你不允许用纹理"着色",但我最近看到的应用程序可以做到这一点.我错过了什么吗?

谢谢.

Zor*_*mic 8

将2个文件添加到项目中,调用它们UINavigationBar-CustomTexture.h,UINavigationBar-CustomTexture.m例如,在这些文件中放置:

.h文件:

#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomTexture)
@end
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

在实例化导航控制器的地方包含.h文件.将您的png文件设置为320x44.根据您的纹理,将导航栏的色调颜色更改为这样(使导航栏上的按钮看起来更好):

[self.navigationController.navigationBar setTintColor:[UIColor colorWithHue:0 saturation:0 brightness:0.5f alpha:0.1f]];
Run Code Online (Sandbox Code Playgroud)

  • 一般来说,类别不是处理自定义绘图的好方法.您将覆盖UINavigationBar的标准实现(如果有)并且永远不会调用该实现.子类化是处理它的正确方法,但它需要更多的工作来设置xib文件中导航栏的类,但更具有未来性. (2认同)