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

After setting UITextField & UITextView cursor color in appDelegate, suddenly I am facing that there is a lot of NavigationItemBar image turn into default Blue color. It's actual color is not showing. The solution is to set each NavigationItemBar image rapped up with this, UIImageRenderingModeAlwaysOriginal

Like:

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

I am just wondering is there any way to set this UIImage property (UIImageRenderingModeAlwaysOriginal) in appDelegate? So that I don't have to change every place in my project, rather set in one place.

Thanks a lot in advance.

Tulon
  • 4,011
  • 6
  • 36
  • 56

2 Answers2

2

Import the obj-c runtime.

<objc/runtime.h>

Declare the following C function to swizzle a method.

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);
}

Declare a new method to swizzle into a category of UIImage

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

Override +load in the UIImage category

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

Import your category.

Now every time you use +imageNamed, it will change the rendering mode automatically.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • Phew, that took a long while to type. – Schemetrical May 27 '15 at 06:46
  • Many many thanks for your answer. Actually I already start my change manually in every place. But your answer looks like perfect to me. And you also refer the link. Which is awesome. Thanks man for your time. :) – Tulon May 27 '15 at 06:48
0

Create the custom UIBarButtonItem and try.

@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

This is working for me.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
Dhiraj
  • 75
  • 3