0

I have an app I've been working on for a while that is all SwiftUI and runs on macOS, iOS, and iPadOS. One feature is a simple address lookup that allows the user to either enter an address or do a search in their Contacts app. The problem is, while a search works fine, and if you have a keyboard, you can hit tab and enter a first name, etc. going between fields in the SwiftUI view, you cannot use any pointer to select any fields on in the view. The following code is the Swift View.

var body: some View {
    NavigationView {
        GeometryReader { geomtry in
            VStack {
                TextField("Address Line 1", text: $addressLine1)
                    .customTextField()
                Spacer()
            }
            ContactPicker(showPicker: $showPicker, onSelectContact: {contact in
                firstName = contact.givenName
                lastName = contact.familyName
                if contact.postalAddresses.count > 0 {
                    if let addressString = (
                        ((contact.postalAddresses[0] as AnyObject).value(forKey: "labelValuePair")
                            as AnyObject).value(forKey: "value"))
                        as? CNPostalAddress {
                        // swiftlint:disable:next line_length
                        let mailAddress = CNPostalAddressFormatter.string(from: addressString, style: .mailingAddress)
                        addressLine1 = "\(addressString.street)"
                    }
                }
                self.showPicker.toggle()
            }, onCancel: nil)
        }
        .padding([.leading, .trailing], 10 )
        .navigationTitle("Recipient")
        .navigationBarItems(trailing:
                                HStack {
                                    Button(action: {
                                            self.showPicker.toggle()
                                    }, label: {
                                        Image(systemName: "magnifyingglass")
                                    })
                                }
        )
    }
}

` I have confirmed that if I disable the ContactPicker function the screen allows me to select fields.. so it must be caused by the ShowPicker capabilities.

Michael Rowe
  • 870
  • 2
  • 11
  • 27
  • Welcome to SO - Please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask) to improve, edit and format your questions. Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. – lorem ipsum Dec 06 '21 at 02:26
  • I've been using the code represented here - https://stackoverflow.com/questions/57246685/uiviewcontrollerrepresentable-and-cncontactpickerviewcontroller/57621666#57621666 as created by @youjinp, The code above has been refactored to be minimal code. – Michael Rowe Dec 18 '21 at 23:12

1 Answers1

0

Ok, this was none intuitive but I have resolved the issue. From what I can tell, the sequence of the ContactPicker and the VStack are important. By moving the ContactPicker to the top of the View, the issue has been resolved.

Michael Rowe
  • 870
  • 2
  • 11
  • 27