在CNContactPickerViewController中隐藏或禁用搜索栏

时间:2017-07-21 03:44:01

标签: ios swift

我发现futex中的搜索栏不允许选择搜索到的联系人。我还看了关于这个问题的其他堆栈溢出问题。这似乎是iOS的bug。但我想知道,有没有办法隐藏或禁用CNContactPickerViewController的搜索栏?因为如果这不起作用,那么我不想展示它。

2 个答案:

答案 0 :(得分: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 + ("> "))
    }
}

在选择器之后调用上面的方法如下:

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: "> ")
}

答案 1 :(得分:0)

正如您发现的here所示,仅当您对CNContactPickerViewController使用多触点选择模式时,问题似乎仍然存在。实施单触点选择模式后,您可以搜索联系人并选择它而不会出现问题。为了使用单触点选择模式,请确保您在CNContactPickerDelegate 方法中实现此方法:

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

而不是此方法:

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

使用这两种方法都可以使用多选模式。