3

I came to know that search bar in CNContactPickerViewController doesn't allow to select searched contacts. I also look other stack overflow questions regarding this issue. It seems like iOS bug. But I want to know, is there any way to hide or disable search bar from CNContactPickerViewController? Because if this doesn't work then I don't want to show it.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116

2 Answers2

0
private var foundSearchBar: Bool = false

func findSearchBar(_ parent: UIView, mark: String) {
    for v: UIView in parent.subviews {
        //if( foundSearchBar ) return;
        print("\(mark)\(NSStringFromClass(v.self))")
        if (v is UISearchBar) {
            (v as? UISearchBar)?.tintColor = UIColor.black
            v.hidden = true
            //            foundSearchBar = YES;
            break
        }
        if (v is UITableView) {
            let temp: CGRect = v.frame
            temp.origin.y = temp.origin.y - 44
            temp.size.height = temp.size.height + 44
            v.frame = temp
            //foundSearchBar = YES;
            break
        }
        findSearchBar(v, mark: mark + ("> "))
    }
}

call above method after picker is presented as below:

func showPeoplePickerController() {
    let picker = ABPeoplePickerNavigationController()
    picker.peoplePickerDelegate = self
    picker.view.autoresizingMask = .flexibleHeight
        // Display only a person's phone, email, and birthdate
    let displayedItems: [Any] = [Int(kABPersonPhoneProperty), Int(kABPersonEmailProperty), Int(kABPersonBirthdayProperty), Int(kABPersonAddressProperty)]
    picker.displayedProperties = displayedItems
    // Show the picker
    present(picker, animated: true) { _ in }
    findSearchBar(picker.view(), mark: "> ")
}
Saurabh Jain
  • 1,688
  • 14
  • 28
0

As you can find here the problem seems to be present only if you use the multi-contact selection mode for the CNContactPickerViewController. Implementing the single-contact selection mode you can search for a contact and select it without problems. In order to use the single-contact selection mode make sure you implement in your CNContactPickerDelegate ONLY this method:

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact)

and not this method:

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) 

Implementing both methods the multi-selection mode is used.

Andrea Gorrieri
  • 1,704
  • 2
  • 22
  • 36