自定义UINavigationBar外观以在CNContactPickerViewController中显示

时间:2016-07-10 12:05:11

标签: ios uinavigationbar uiappearance

我使用下面的代码来自定义我的UINavigationBar的外观,当它显示ContactPickerViewController时:

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

但未显示任何更改。哪里错了?

2 个答案:

答案 0 :(得分:1)

我已经为UIViewController创建了一个类别,只有在您使用方法调整来呈现UINavigationBar时才能自定义CNContactPickerViewController的外观。在演示之前,我会检查演示文稿是否为CNContactPickerViewController,然后我们会更改外观。然后在解雇时,我将外观重置为默认值。它疯狂的解决方案,但它完成了工作。

#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

答案 1 :(得分:0)

UINavigationBar始终位于UINavigationController。您不应该使用UINavigationController appearance来控制它。