0

I link a simple project to explain my problem.

In simple terms, I have a navigation controller based app with red theme and I have to pick email addresses from my contacts via CNContactPickerViewController. img 1

My problem is with back bar button of contact detail view (red circle in image below): it appear white in a white background, so it is invisible.

img 2

How can i change the tint color of back bar button in the default one ( for contact detail view only)?

Giorgio
  • 1,973
  • 4
  • 36
  • 51
  • Did you find how to change the tint color ? – Grifas Jul 24 '17 at 12:45
  • No, I'm sorry. Please, answer my question if you'll find a solution. – Giorgio Jul 25 '17 at 07:40
  • I'm not sure but I think it's possible with a subclass of CNContactPickerViewController. Use `self.navigationController.navigationBar.tintColor = UIColor.white` in the viewDidLoad – Grifas Jul 25 '17 at 07:55

1 Answers1

0

I didn't find a way to change the UIBarButtonItem tintColorin 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.

Giorgio
  • 1,973
  • 4
  • 36
  • 51
  • 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:38
  • @Argus if you do not use custom UINavigationBar.appearance() in you project you don't need to restore the default iOS UINavigationBar appearance and then you don't need this workaround. – Giorgio Dec 09 '19 at 13:46