Ios 是否有任何方法可以在整个项目的所有UIImage属性的appdelegate中使用“UIImageRenderingModeAlwaysOriginal”
在appDelegate中设置Ios 是否有任何方法可以在整个项目的所有UIImage属性的appdelegate中使用“UIImageRenderingModeAlwaysOriginal”,ios,objective-c,iphone,uiimage,Ios,Objective C,Iphone,Uiimage,在appDelegate中设置UITextField和UITextView光标颜色后,我突然发现有很多NavigationItemBar图像变成了默认的蓝色。它的实际颜色没有显示出来。解决方案是将每个NavigationItemBar图像设置为与此关联,UIImageRenderingModeAlwaysOriginal 比如: 我只是想知道有没有办法在appDelegate中设置这个UIImage属性(uiimagerenderingmodealwaysororiginal)?这样我就不必改变
UITextField
和UITextView
光标颜色后,我突然发现有很多NavigationItemBar
图像变成了默认的蓝色。它的实际颜色没有显示出来。解决方案是将每个NavigationItemBar
图像设置为与此关联,UIImageRenderingModeAlwaysOriginal
比如:
我只是想知道有没有办法在appDelegate
中设置这个UIImage
属性(uiimagerenderingmodealwaysororiginal
)?这样我就不必改变我项目中的每一个地方,而只需在一个地方进行设置
提前非常感谢。创建自定义的
uibarbuttoneim
并尝试
`[[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];`
这对我有用。导入obj-c运行时
@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
声明一个新方法以切换到UIImage
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
类别中的+load
+ (instancetype)renderingMode_imageNamed:(NSString *)imageName {
UIImage *image = [UIImage renderingMode_imageNamed:imageName];
return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
导入您的类别
现在,每次使用
+imageNamed
,它都会自动更改渲染模式。我不认为存在任何这样的方法:(我使用了一个混乱的解决方案,但效果很好。参考和我的答案。他想要一种全局设置的方法,而不是单独设置。呸,这花了很长时间才输入。非常感谢你的答案。实际上我已经在每个地方手动启动了更改。但是你的答案看起来很完美。你还参考了链接。这太棒了。谢谢你的帮助。)我们的时代。:)
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);
}
+ (instancetype)renderingMode_imageNamed:(NSString *)imageName {
UIImage *image = [UIImage renderingMode_imageNamed:imageName];
return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SwizzleClassMethod([UIImage class], @selector(imageNamed:), @selector(renderingMode_imageNamed:))
})
}