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

发布于 2024-08-26 14:12:02 字数 213 浏览 11 评论 0原文

我尝试这样做:

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

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

谢谢。

I tried doing this:

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

but it just ended up black. At first I assumed you weren't allowed to "tint" with a texture, but I've seen apps recently that can do this. Am I missing something?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林很绿却致人迷途 2024-09-02 14:12:02

将 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]];

Add 2 files to your project, call them UINavigationBar-CustomTexture.h and UINavigationBar-CustomTexture.m for example, in those files put this:

.h file:

#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomTexture)
@end

.m file:

#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

Include the .h file where you instantiate your navigation controller.
Make your png file 320x44. Depending on your texture, change the tint color of your navigation bar to something like this (to make the buttons on the navigation bar look better):

[self.navigationController.navigationBar setTintColor:[UIColor colorWithHue:0 saturation:0 brightness:0.5f alpha:0.1f]];
卸妝后依然美 2024-09-02 14:12:02

这是我解决这个问题的方法:

_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];

Here is how I got around this problem:

_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];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文