2016-07-10 22 views
2

ich Code unten verwenden Sie das Aussehen meiner UINavigationBar zu gestalten, wenn es eine ContactPickerViewController zeigt:anpassen UINavigationBar Aussehen für die Ansicht in CNContactPickerViewController

id specialNavBarAppearance = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[CNContactPickerViewController class]]]; 

    [specialNavBarAppearance setBarTintColor:[UIColor colorWithRed:213/255.0f green:38/255.0f blue:46/255.0f alpha:1.0]]; 
    [specialNavBarAppearance setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: 
                  [UIColor whiteColor], NSForegroundColorAttributeName,nil]]; 
    [specialNavBarAppearance setTintColor:[UIColor whiteColor]]; 

aber keine Änderungen angezeigt. wo liege ich falsch?

+0

Können Sie zuerst sicherstellen, dass 'specialNavBarAppearance' nicht null ist? –

+0

geprüft, es ist nicht null –

Antwort

0

UINavigationBar ist immer in UINavigationController. Sie sollten UINavigationController appearance nicht verwenden, um dies zu steuern.

+0

so wie können wir es anpassen in bestimmten ViewControllern wie CNContactPickerViewController –

1

Ich habe eine Kategorie für UIViewController erstellt das Aussehen von UINavigationBar nur anpassen, wenn Sie CNContactPickerViewController mit Methode Swizzling präsentieren. Vor dem Präsentieren prüfe ich, ob es sich bei dem vorgestellten CNContactPickerViewController handelt, dann ändern wir das Aussehen. Beim Zurücksetzen setzte ich das Aussehen auf die Standardwerte zurück. Seine verrückte Lösung, aber es macht die Arbeit erledigt.

#import "UIViewController+CustomAppearance.h" 
#import <objc/runtime.h> 
@import ContactsUI; 

@implementation UIViewController (CustomAppearance) 

+ (void)load { 
static dispatch_once_t onceToken; 
dispatch_once(&onceToken, ^{ 
    Class class = [self class]; 

    SEL originalSelector = @selector(presentViewController:animated:completion:); 
    SEL swizzledSelector = @selector(presentViewController2:animated:completion:); 

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



    SEL originalSelector2 = @selector(dismissViewControllerAnimated:completion:); 
    SEL swizzledSelector2 = @selector(dismissViewControllerAnimated2:completion:); 

    Method originalMethod2 = class_getInstanceMethod(class, originalSelector2); 
    Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2); 

    BOOL didAddMethod2 = 
    class_addMethod(class, 
        originalSelector2, 
        method_getImplementation(swizzledMethod2), 
        method_getTypeEncoding(swizzledMethod2)); 

    if (didAddMethod2) { 
     class_replaceMethod(class, 
          swizzledSelector2, 
          method_getImplementation(originalMethod2), 
          method_getTypeEncoding(originalMethod2)); 
    } else { 
     method_exchangeImplementations(originalMethod2, swizzledMethod2); 
    } 

}); 
} 

#pragma mark - Method Swizzling 

- (void)dismissViewControllerAnimated2:(BOOL)flag completion:(void (^)(void))completion 
{ 
    [self setupDefualtAppearance]; 

    [self dismissViewControllerAnimated2:flag completion:completion]; 
} 



- (void)presentViewController2:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion { 

    if ([viewControllerToPresent isKindOfClass:[CNContactPickerViewController class]]) { 
    [self setupContactsPickerAppearance]; 

} 
    [self presentViewController2:viewControllerToPresent animated:flag completion:completion]; 
} 


- (void)setupDefualtAppearance{ 

    id specialNavBarAppearance = [UINavigationBar appearance]; 

    [specialNavBarAppearance setBarTintColor:nil]; 
    [specialNavBarAppearance setTitleTextAttributes: nil]; 
    [specialNavBarAppearance setTintColor:nil]; 
    } 


    - (void)setupContactsPickerAppearance{ 
    id specialNavBarAppearance = [UINavigationBar appearance]; 

    [specialNavBarAppearance setBarTintColor:[UIColor colorWithRed:213/255.0f green:38/255.0f blue:46/255.0f alpha:1.0]]; 
    [specialNavBarAppearance setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: 
                [UIColor whiteColor], NSForegroundColorAttributeName,nil]]; 
    [specialNavBarAppearance setTintColor:[UIColor whiteColor]]; 
    } 

    @end