首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI - CNContactViewController NavigationBar问题

SwiftUI - CNContactViewController NavigationBar问题
EN

Stack Overflow用户
提问于 2020-03-19 06:30:00
回答 2查看 246关注 0票数 2

我正在尝试实现CNContactViewDelegate,以便能够显示CNContact的细节。显然,我是第一个用SwiftUI实现它的人,但遇到了问题。无论如何,我可以看到使用UIViewControllerRepresentableCNContact的细节,但我在NavigationBar上有一个问题,在Contact的图像和StatusBar -because的NavigationBarNavigationLink之间存在差距-这种差距不存在于原生Contacts应用程序中,显然是在< link >D12中实现框架的这个代码中。

以下是代码;

代码语言:javascript
复制
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)都不能工作,这里的一个例子是原生应用的屏幕截图;

这是我的代码的结果;

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-18 18:19:59

由于这个问题获得了赞成票,我想我可以分享我的中途解决方案。这解决了差距,然而,在过渡到细节的过程中,导航栏的背景色有一个小故障。在过渡之后,这一点变得清晰起来。

代码语言:javascript
复制
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
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-05-09 08:15:21

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

代码语言:javascript
复制
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
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60748395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档