作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试这样做:
[toolbar setTint:[UIColor colorWithPatternImage:[UIImage imageNamed:@"thingFromMyBundle.png"]]];
但它最终变成了黑色。起初我以为你不允许使用纹理“着色”,但我最近看到了可以做到这一点的应用程序。我错过了什么吗?
谢谢。
最佳答案
向您的项目添加 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
包含实例化导航 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/
我是一名优秀的程序员,十分优秀!