1

how do i change the black bar to another color?

N.B this is a CNContactPickerViewcontroller...the first screen(list of contacts) looks fine but when i click on a contact to choose a specific contact property then the navigation bar turns black.

Thanks

enter image description here

2 Answers2

3

One way is to set the Appearance of UINavigationBar to the color you want:

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

And once you return to the previous ViewController (Maybe with -(void)viewWillAppear:(BOOL)animated) you set it again to the previous color you were using.

I presented the Contact this way:

CNContactStore *store = [[CNContactStore alloc] init];

// Create contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
contact.givenName = @"Someone Name";

CNLabeledValue *contactPhoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:@"Some number"]];
contact.phoneNumbers = @[contactPhoneNumber];

CNContactViewController *contactController = [CNContactViewController viewControllerForUnknownContact:contact];
contactController.navigationItem.title = @"Add to contacts";
contactController.contactStore = store;
contactController.delegate = self;


[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

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

self.navigationController.navigationBar.tintColor = [UIColor blackColor];

self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};

[self.navigationController pushViewController:contactController animated:YES];

Once the contactController is Dismissed the viewWillAppear is called and you can add there the color restore depending on your needs:

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

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

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};

}

Luis Mejías
  • 301
  • 2
  • 10
  • 1
    Okay, because my experience is that you don't get control over the second CNContactPickerViewcontroller screen this way (because it isn't part of your app). – matt Feb 03 '17 at 20:04
  • Yes, sounds similar to my answer here: http://stackoverflow.com/a/40600727/341994 — thanks. – matt Feb 03 '17 at 20:10
  • if you do not use UINavigationBar.appearance() in you project that solution will confuse nav bar colors across whole app. – Argus Dec 09 '19 at 13:40
1

I've solved it by changing the backgroundColor of the navbar subviews after a delay, not pretty, but it's good enough for me:

CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];
vc.delegate = self;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    for (UIView *view in [vc.navigationController.navigationBar subviews]) {
        view.tintColor = [UIColor darkTextColor];

        view.backgroundColor = [UIColor redColor];
    }
});

[self.navigationController pushViewController:vc animated:YES];
MauritsK
  • 51
  • 2