iphone - 我可以将纹理应用到 UIToolbar 吗?

标签 iphone colors textures uitoolbar

我尝试这样做:

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

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

谢谢。

最佳答案

向您的项目添加 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

包含实例化导航 Controller 的 .h 文件。 将您的 png 文件设为 320x44。根据您的纹理,将导航栏的色调更改为类似这样的颜色(以使导航栏上的按钮看起来更好):

[self.navigationController.navigationBar setTintColor:[UIColor colorWithHue:0 saturation:0 brightness:0.5f alpha:0.1f]];

关于iphone - 我可以将纹理应用到 UIToolbar 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2520619/

相关文章:

java - 在java Graphics对象中设置背景颜色

html - css:UL 类中的链接颜色不会覆盖 div 的默认颜色

rendering - 重心坐标纹理映射

xna - 什么时候加载纹理?

python - 如何用 3 个数组制作彩色 2D 网格

javascript - 在 Three.js 中从 Web Worker 加载纹理

iphone - 更改 UITableViewCell 的编辑控件

iphone - Viewcontroller 失败所以调用另一个 Viewcontroller - Objective-c

iphone - 检测音乐是否正在播放?

ios - 具有 map 应用程序行为的搜索栏(选择时导航栏全宽)