目标是在 iOS 上添加新联系人时仅显示某些字段。
例如,假设您只想显示和编辑联系人的地址、电话号码和名字。
下面的代码不起作用。所有字段仍然出现。
将此视图控制器放入项目中,您可以看到所有联系人字段仍然显示,尽管使用displayedPropertyKeys.
你会怎么做?
import Foundation
import Contacts
import ContactsUI
class ContactViewController: UIViewController, CNContactViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func createContact() {
let contactController = CNContactViewController(forNewContact: nil)
contactController.delegate = self
contactController.allowsEditing = true
contactController.allowsActions = true
contactController.displayedPropertyKeys = [CNContactPostalAddressesKey, CNContactPhoneNumbersKey, CNContactGivenNameKey]
contactController.view.layoutIfNeeded()
present(UINavigationController(rootViewController: contactController), animated:true)
}
// =============================================================================================================
// MARK: IB Actions
// =============================================================================================================
@IBAction func newContactButtonDidTap(_ sender: UIButton) {
createContact()
}
// =============================================================================================================
// MARK: UIViewController Functions
// =============================================================================================================
override var prefersStatusBarHidden: Bool {
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}