Reputation: 183
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
Upvotes: 1
Views: 3539
Reputation: 51
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];
Upvotes: 1
Reputation: 301
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]};
}
Upvotes: 3