Our tint color is white. Our app uses CNContactViewController. In our version of the app in the store built with Xcode 7 targeting iOS 8 and 9, if you were iOS 9 we called
By using Debug View Hierarchy of XCode, I find the alpha of the subview named "_UIBarBackground" of UINavigationBar turns to be 0 after CNContactViewController has been pushed.
The following code helps me solve the problem (It works well in iOS 11):
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
for (UIView *view in self.navigationController.navigationBar.subviews) {
if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
view.alpha = 1;
break;
}
}
});
I was having the exact same issue. It definitely seems like an iOS 10 bug. Anyways, I found a work around by setting the navigation bar's translucency to false. Then set the background color of the application's main window to whatever color you want the navigation bar to be.
Some code snippets:
UINavigationBar.appearance().isTranslucent = false
UIApplication.shared.delegate?.window??.backgroundColor = UIColor.red
In Swift 5 and Xcode 10.2
In iOS 9.0 CNContactViewController navigation bar working properly, but not higher versions.
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
//Set status bar background colour
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
//Set navigation bar subView background colour
for view in controller.navigationController?.navigationBar.subviews ?? [] {
view.tintColor = UIColor.white
view.backgroundColor = UIColor.red
}
})
navigationController?.pushViewController(controller, animated: true)
Here i fixed status bar background colour and navigation bar background colour. If you don't want status bar colour comment it.
The complete code is
func addPhoneNumber(phNo:String) {
if #available(iOS 9.0, *) {
let store = CNContactStore()
let contact = CNMutableContact()
let homePhone = CNLabeledValue(label: CNLabelHome, value: CNPhoneNumber(stringValue : phNo))
contact.phoneNumbers = [homePhone]
let controller = CNContactViewController(forUnknownContact : contact)
controller.contactStore = store
controller.delegate = self
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
//Set status bar background colour
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
//Set navigation bar subView background colour
for view in controller.navigationController?.navigationBar.subviews ?? [] {
view.tintColor = UIColor.white
view.backgroundColor = UIColor.red
}
})
navigationController?.pushViewController(controller, animated: true)
}
}
I've solved it like this:
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];
Your question has solved my problem: I now know why I have the same issue.
I have resolved it by setting navigationController.navigationBar.tintColor to a shade of blue just before pushing the CNContactViewController. On exit (in the delegate method) set it back to white.