有什么方法可以在 appdelegate 中为项目中的所有 UIImage 属性 使用 `UIImageRenderingModeAlwaysOriginal`

Is there any way to use `UIImageRenderingModeAlwaysOriginal` in appdelegate for all UIImage Property across the project

[[UITextField appearance] setTintColor:[UIColor myColor]];
[[UITextView appearance] setTintColor:[UIColor myColor]];

在 appDelegate 中设置 UITextFieldUITextView 光标颜色后,突然我发现有很多 NavigationItemBar 图像变成默认的蓝色。它的实际颜色没有显示出来。解决方案是设置每个 NavigationItemBar 图像,UIImageRenderingModeAlwaysOriginal

喜欢:

`[[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];`

我只是想知道有什么方法可以在 appDelegate 中设置这个 UIImage 属性 (UIImageRenderingModeAlwaysOriginal) 吗?这样我就不必更改项目中的每个地方,而是在一个地方进行设置。

非常感谢。

创建自定义 UIBarButtonItem 并尝试。

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.navigationItem setTitle:@"Sample title"];
    UIImageView *customView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [customView setImage:[UIImage imageNamed:@"SettingIcon"]];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:customView];
    [self.navigationItem setRightBarButtonItem:right];
}

@end

这对我有用。

导入 obj-c 运行时。

<objc/runtime.h>

声明以下 C 函数以调配方法。

void SwizzleClassMethod(Class c, SEL orig, SEL new) {

    Method origMethod = class_getClassMethod(c, orig);
    Method newMethod = class_getClassMethod(c, new);

    c = object_getClass((id)c);

    if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
        class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    else
        method_exchangeImplementations(origMethod, newMethod);
}

声明一个新方法以混入 UIImage

类别
+ (instancetype)renderingMode_imageNamed:(NSString *)imageName {
    UIImage *image = [UIImage renderingMode_imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

覆盖 UIImage 类别中的 +load

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SwizzleClassMethod([UIImage class], @selector(imageNamed:), @selector(renderingMode_imageNamed:))
    })
}

导入您的类别。

现在每次使用+imageNamed,都会自动改变渲染模式。