现在我正在全球范围内改变AppDelegate中的bartint颜色。
[[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
有没有办法保留这个,但是当这些视图以模态方式呈现时全局改变BarTintColor?
答案 0 :(得分:0)
所以我决定创建一个使用viewWillAppear来解决这个问题的类别。
@implementation UIViewController (IsModal)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(viewWillAppear:);
SEL swizzledSelector = @selector(extended_viewWillAppear:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)extended_viewWillAppear:(BOOL)animated {
[self extended_viewWillAppear:animated];
[self styleIfModal];
}
- (void)styleIfModal {
if([self isModal] && self.navigationController != nil) {
[self.navigationController.navigationBar setBarTintColor:[UIColor grayColor]];
}
}
- (BOOL)isModal
{
return self.presentingViewController.presentedViewController == self || (self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController) || [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
}