I didn't find a way to change the UIBarButtonItem
tintColor
in the contact detail view, so I solved the problem of white bar button color in white background restoring the default UINavigationBar
appearance
just before CNContactPickerViewController
presentation and changing back to my custom UINavigationBar
appearance
when CNContactPickerViewController
is dismissed.
import UIKit
import Contacts
import ContactsUI
class ViewController: UIViewController, CNContactPickerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func openContacts(_ sender: Any) {
let contactPicker = CNContactPickerViewController()
contactPicker.predicateForSelectionOfContact = NSPredicate(value: false)
contactPicker.delegate = self
setupDefualtAppearance()
self.present(contactPicker, animated: true, completion: nil)
}
func setupCustomAppearance() {
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor(red: 175.0/255.0, green: 22.0/255.0, blue: 28.0/255.0, alpha: 1.0)
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().barStyle = .black
}
func setupDefualtAppearance() {
UINavigationBar.appearance().tintColor = nil
UINavigationBar.appearance().barTintColor = nil
UINavigationBar.appearance().titleTextAttributes = nil
UINavigationBar.appearance().barStyle = .default
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
setupCustomAppearance()
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
setupCustomAppearance()
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
setupCustomAppearance()
}
}
I link a simple project to explain the workaround.