Aldo Lazuardi Aldo Lazuardi - 4 months ago 20
Swift Question

change title for contactsUI 'forNewContact' swift

I'm using contacts UI for adding new contact with this code

let a = CNContact()
let contactPickerViewController = CNContactViewController.init(forNewContact: a) // iOS Editor
contactPickerViewController.delegate = self
let nav = UINavigationController.init(rootViewController: contactPickerViewController)
nav.title = "AppContact"
self.present(nav, animated: true, completion: nil)


I want to make the nav bar title to be "AppContact" but it always get the default "New Contact". How to change the title?

Answer

The navigation bar grabs the title from the UIViewController that is presently on top of the navigation stack, In your case the RootViewController. Instead change the title property of that UIViewController

    let a = CNContact()
    let contactPickerViewController = CNContactViewController.init(forNewContact: a) // iOS Editor
    contactPickerViewController.delegate = self
    let nav = UINavigationController.init(rootViewController: contactPickerViewController)
    contactPickerViewController.title = "AppContact" //Using currently presented ViewController .title property.
    self.present(nav, animated: true, completion: nil)

This is the discussion of the title property of from the UIViewController class reference provided by apple

Discussion:

Set the title to a human-readable string that describes the view. If the view controller has a valid navigation item or tab-bar item, assigning a value to this property updates the title text those objects.

Link to UIViewController title documentation.