CNContactViewController navigation bar colour not working properly

804 Views Asked by At

CNContactViewController navigation bar colour not appearing when i click Create New Contact option. See my screens for 1st time it's ok, but when i click Create New Contact i'm not getting navigation bar colour and not visible back button.

1st screen

enter image description here

2nd screen

enter image description here

In older versions

enter image description here

My code is

if #available(iOS 9.0, *) {
        let store = CNContactStore()
        let contact = CNMutableContact()
        let homePhone = CNLabeledValue(label: CNLabelHome, value: CNPhoneNumber(stringValue : self.mobile ?? ""))
        contact.phoneNumbers = [homePhone]
        let controller = CNContactViewController(forUnknownContact : contact)
        controller.contactStore = store
        controller.delegate = self

        if #available(iOS 10.0, *) {
            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)
    }

And one more is by default phone number: (913) 351-5518

1

There are 1 best solutions below

0
Giuseppe Beccari On

I would suggest to display a CNConctactViewController within a UINavigationController in Popover Modal Presentation style, and add a few button to return to the main application. This implementation seams not trivial as reported by rumours over the net.

Let me share a few pieces of my code (swift 5).

The major class:

class MyViewController: UITableViewController, UIPopoverPresentationControllerDelegate {

   var contactViewController = CNContactViewController()
   ...
   @objc func dismissContactViewController() {
      contactViewController.dismiss(animated: true, completion: nil)
   }
}

The extension:

extension MyViewController: CNContactViewControllerDelegate {

func openCNContactViewController(willAppearWith: CNContact, type: ContactType) {
    switch type {
    case .forContact:
        contactViewController = CNContactViewController(for: willAppearWith)
        contactViewController.allowsEditing = false
        break
    case .forNewContact:
        contactViewController = CNContactViewController(forNewContact: willAppearWith)
        contactViewController.allowsEditing = true
        break
    case .forUnknowContact:
        contactViewController = CNContactViewController(forUnknownContact: willAppearWith)
        contactViewController.allowsEditing = true
        break
    }
    contactViewController.allowsActions = true
    contactViewController.contactStore = globalContactStore
    contactViewController.hidesBottomBarWhenPushed = true
    contactViewController.delegate = self
    // define the button (or select a default one)
    let button = UIButton(type: .custom)
    button.setTitleColor(self.view.tintColor, for: .normal)
    button.setTitle("My app name", for: .normal)
    button.addTarget(self, action: #selector(dismissContactViewController), for: .touchUpInside)
    let closeButton = UIBarButtonItem(customView: button)
    closeButton.style = .plain

    // add flexible space
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)

    // add to toolbar
    contactViewController.setToolbarItems([flexibleSpace, closeButton, flexibleSpace], animated: false)

    let navigationVC = UINavigationController(rootViewController: contactViewController)

    // show toolbar
    navigationVC.setToolbarHidden(false, animated: false)

    // set navigation presentation style
    navigationVC.modalPresentationStyle = .popover

    // present view controller
    self.present(navigationVC, animated: true, completion: nil)
 }

The result enter image description here