1

当我从联系人卡片按编辑时,我的 CNContactViewController 没有在屏幕底部显示删除选项。

注意:按钮在 iOS 13 中仍然显示。

截屏

4

2 回答 2

0
import Foundation
import ContactsUI
import SwiftUI

struct CNContactViewControllerRepresentable: UIViewControllerRepresentable {
typealias UIViewControllerType = CNContactViewController
    var contact: Binding<CNContact>
    var presentingEditContact: Binding<Bool>

    func makeCoordinator() -> CNContactViewControllerRepresentable.Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<CNContactViewControllerRepresentable>) -> CNContactViewControllerRepresentable.UIViewControllerType {
        let controller = CNContactViewController(forNewContact: contact.wrappedValue)
        controller.delegate = context.coordinator
        return controller
    }

    func updateUIViewController(_ uiViewController: CNContactViewControllerRepresentable.UIViewControllerType, context: UIViewControllerRepresentableContext<CNContactViewControllerRepresentable>) {
        //
    }

    // Nested coordinator class, the prefered way stated in SwiftUI documentation.
    class Coordinator: NSObject, CNContactViewControllerDelegate {
        var parent: CNContactViewControllerRepresentable

        init(_ contactDetail: CNContactViewControllerRepresentable) {
            self.parent = contactDetail
        }

        func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
            parent.contact.wrappedValue = contact ?? parent.contact.wrappedValue
            parent.presentingEditContact.wrappedValue = false
        }

        func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
            return true
        }
    }
}
于 2020-10-20T17:10:16.850 回答
0
.sheet(isPresented: $viewModel.presentingEditContact) {
            NavigationView {
                if #available(iOS 14, *) {
                    return AnyView(CNContactViewControllerRepresentable(contact: self.$viewModel.contact, presentingEditContact: $viewModel.presentingEditContact)
                        .navigationBarTitle("Edit Contact")
                        .edgesIgnoringSafeArea(.top))
                } else {
                    return AnyView(CNContactViewControllerRepresentable(contact: self.$viewModel.contact, presentingEditContact: $viewModel.presentingEditContact)
                        .edgesIgnoringSafeArea(.top))
                }
            }
        }
于 2020-10-20T17:13:54.643 回答