youjinp / SwiftUIKit

A collection of missing SwiftUI components
MIT License
278 stars 32 forks source link

ContactPicker dismisses my sheet #13

Closed hackzilla closed 4 years ago

hackzilla commented 4 years ago

I'm displaying the ContactPicker inside a sheet and it all worked when it was contained in a form. However, I struggled with the form, so I removed it.

Now when you chose a contact, my sheet gets dismissed.

Any ideas?

This code shows the issue that I am facing.

struct ContactPickerDemoView: View {
    @State var contact: CNContact?
    @State var showPicker = false
    @State var showingSheet = false

    var body: some View {
        Button(action: {
            self.showingSheet.toggle()
        }) {
            Text("Show Sheet")
        }
        .sheet(isPresented: $showingSheet) {
            ZStack {
                ContactPicker(
                    showPicker: $showPicker,
                    onSelectContact: {c in
                        self.contact = c})
                VStack{
                    Spacer()
                    Button(action: {
                        self.showPicker.toggle()
                    }) {
                        Text("Pick a contact")
                    }
                    Spacer()
                    Text("\(self.contact?.givenName ?? "")")
                    Spacer()
                }
            }
        }
    }
}
youjinp commented 4 years ago

Yeah that's beyond my control unfortunately. From what I recall CNContactPickerViewController comes bundled with it's own sheet, that's maybe the reason why it dismissed your sheet.

You have a couple of options:

  1. don't present CNContactPickerViewController from a sheet
  2. implement your own contactPickerViewController
hackzilla commented 4 years ago

I was worried that you'd say that. I'll do the first option.

Thank you for your library, it is helpful.