[[UITextField appearance] setTintColor:[UIColor myColor]];
[[UITextView appearance] setTintColor:[UIColor myColor]];
设置UITextField
& appDelegate中的UITextView
光标颜色,突然我面临着很多NavigationItemBar
图像变成默认的蓝色。它的实际颜色没有显示出来。解决方案是使用此NavigationItemBar
UIImageRenderingModeAlwaysOriginal
图像
像:
`[[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];`
我只是想知道有没有办法在UIImage
中设置此UIImageRenderingModeAlwaysOriginal
属性(appDelegate
)?这样我就不必改变项目中的每个地方,而是设置在一个地方。
提前多多感谢。
答案 0 :(得分:2)
导入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];
}
覆盖+load
类别
UIImage
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SwizzleClassMethod([UIImage class], @selector(imageNamed:), @selector(renderingMode_imageNamed:))
})
}
导入您的类别。
现在每次使用+imageNamed
时,它都会自动更改渲染模式。
答案 1 :(得分:0)
创建自定义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
这对我有用。