2016-11-08 9 views
0

今のところ、私はAppDelegateのbartintの色をこのようにグローバルに変更しています。異なるUINavigationBar BarTintの色が表示される場合Modured

[[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]]; 

これを保持する方法はありますが、これらのビューがモーダルで表示されている場合は、BarTintColorをグローバルに変更しますか?

+0

ビューをモーダルに表示してビューを変更し、ビューが正しく終了したときに変更することはできますか? – KrishnaCA

+0

私は、複数のクラスを変更するのではなく、グローバルな解決策を望んでいます。 – puttputt

答えて

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