[英]Different UINavigationBar BarTint Color when presented Modally
Right now I am globally changing the bartint color in the AppDelegate like this. 现在,我正在像这样全局更改AppDelegate中的bartint颜色。
[[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
Is there a way to keep this, but globally alter the BarTintColor when those views are presented modally? 有没有办法保持这种状态,但是当这些视图以模态显示时,会全局更改BarTintColor?
So I decided to create a category that swizzled viewWillAppear to solve this. 因此,我决定创建一个使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]];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.