为 UIBezierPath bezierPathWithRect 设置填充颜色

Setting fill color for UIBezierPath bezierPathWithRect

我有以下UIImage()

+ (UIImage *)defaultImage {
    static UIImage *defaultImage = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 13.f), NO, 0.0f);

        [[UIColor yellowColor] setFill];
        [[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 1)] fill];
        [[UIBezierPath bezierPathWithRect:CGRectMake(0, 5, 20, 1)] fill];

        [[UIColor blueColor] setFill];
        [[UIBezierPath bezierPathWithRect:CGRectMake(0, 10, 20, 1)] fill];
        [[UIBezierPath bezierPathWithRect:CGRectMake(0, 1, 20, 2)] fill];

        [[UIColor greenColor] setFill];
        [[UIBezierPath bezierPathWithRect:CGRectMake(0, 6,  20, 2)] fill];
        [[UIBezierPath bezierPathWithRect:CGRectMake(0, 11, 20, 2)] fill];

        defaultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    });

    return defaultImage;
}

我在这里使用它:

buttonController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[self class] defaultImage] style:UIBarButtonItemStylePlain target:self action:@selector(toggleLeftPanel:)];

问题是 setFill 颜色不起作用。矩形始终为红色。

它们实际上呈现出 UIViewControllernavigationBar.tintColor 的颜色。

self.navigationController.navigationBar.tintColor = [UIColor redColor];

如果我删除 tintColor 那么它们总是蓝色的。

已使用以下内容修复:

buttonController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[[self class] defaultImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(toggleLeftPanel:)];

发生的事情是图像被渲染为 "template",也就是说,只有 alpha 通道被绘制,颜色来自 navigationBartintColor .