SwiftUI - CNContactViewController NavigationBar 问题

SwiftUI - CNContactViewController NavigationBar problem

我正在尝试实现 CNContactViewDelegate 以便能够显示 CNContact 的详细信息。显然,我是第一个使用 SwiftUI 实现它并遇到问题的人。无论如何,我可以通过使用 UIViewControllerRepresentable 看到 CNContact 的细节,但我对 NavigationBar 有疑问,Contact 的图像和 StatusBar - 因为 NavigationBarNavigationLink 我认为 - 原生 Contacts 应用程序中不存在这种差距,显然在 link 中实现了框架UIKit

这是代码;

struct ContactsListView: View {
    @ObservedObject var contactsModel: ContactsViewModel
    var body: some View {
        NavigationView{
            List {
                //After some ForEach's and Section's
                //This view is working. 
                NavigationLink(destination: ContactDetailView(contact: self.$contactsModel.contacts[sectionIdx].contacts[contactIdx])) {
                    Text(self.contactsModel.contacts[sectionIdx].contacts[contactIdx].givenName)
                }
            }
            .navigationBarTitle("Contacts")
        }
    }
}


struct ContactView: UIViewControllerRepresentable {

    @Binding var contact: CNContact

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

    func makeUIViewController(context: UIViewControllerRepresentableContext<ContactView>) -> CNContactViewController {
        let controller = CNContactViewController(for: contact)
        self.navigationBarHidden(true)
        return controller
    }

    func updateUIViewController(_ uiViewController: CNContactViewController, context: UIViewControllerRepresentableContext<ContactView>) {
        print(context)
    }

    class Coordinator: NSObject, CNContactViewControllerDelegate {

        var parent: ContactView

        init(_ contactDetail: ContactView) {
            self.parent = contactDetail
            self.parent.navigationBarHidden(true)

        }
    }
}

ContactView 中,这两个 self.navigationBarHidden(true) 都不起作用。这里的问题示例是本机应用程序的屏幕截图;

这是我的代码的结果;

由于问题获得了赞成票,我想我可以分享我的半途而废的解决方案。这解决了差距,但是在过渡到细节的过程中,带有背景颜色的导航栏出现故障。过渡之后,它变得清晰起来。

struct ContactDetailView:  View {

    var contact: CNContact

    var body: some View {
        ZStack {
            Color.clear
            ContactView(contact: self.contact)
                .navigationBarTitle("", displayMode: .inline)
        }.edgesIgnoringSafeArea(.top)
    }
}

struct ContactView: UIViewControllerRepresentable {

    var contact: CNContact

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

    func makeUIViewController(context: UIViewControllerRepresentableContext<ContactView>) -> CNContactViewController {
        let controller = CNContactViewController(forUnknownContact: contact)
        controller.allowsActions = true
        controller.allowsEditing = false
        controller.delegate = context.coordinator
        return controller
    }

    func updateUIViewController(_ uiViewController: CNContactViewController, context: UIViewControllerRepresentableContext<ContactView>) {
        print("updated")
    }

    class Coordinator: NSObject, CNContactViewControllerDelegate {

        var parent: ContactView

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

        func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
        }

        func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {

            return true
        }
    }
}

发表了我对解决方案的评论,然后我想到了将联系人视图控制器包装在我的自定义 NavigationController 中。瞧,修好了!

struct ContactView: UIViewControllerRepresentable {

    var contact: CNContact

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

    func makeUIViewController(context: UIViewControllerRepresentableContext<ContactView>) -> NavigationController {
        let controller = CNContactViewController(forUnknownContact: contact)
        controller.contactStore = CNContactStore()
        controller.delegate = context.coordinator
        let navigationController = NavigationController(rootViewController: controller)
        return navigationController
    }

    func updateUIViewController(_ uiViewController: NavigationController, context: UIViewControllerRepresentableContext<ContactView>) {
    }

    class Coordinator: NSObject, CNContactViewControllerDelegate {

        var parent: ContactView

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

        func contactViewController(_ viewController: CNContactViewController,
                                   didCompleteWith contact: CNContact?) {
        }

        func contactViewController(_ viewController: CNContactViewController,
                                   shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
            return true
        }
    }
}