2016-11-08 74 views
0

現在我正在全球改變AppDelegate中的bartint顏色,就像這樣。不同的UINavigationBar BarTint顏色當呈現Modally

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

有沒有一種方法來保持這一點,但全球改變BarTintColor時,這些意見模態地呈現?

+0

當您以模態方式呈現視圖並在視圖被解散時將其更改回來時,您可以更改外觀嗎? – KrishnaCA

+0

對。我希望有一個全球性的解決方案,而不是修改多個類。 – puttputt

回答

0

因此,我決定創建一個類似swizzled 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]]; 
}