SwiftUI: Popover dismiss itself when the keyboard cover the anchor after a first value edition

155 Views Asked by At

I'm facing a strange problem with popover on SwiftUI that I don't understand: I have a list of entries where I want to edit list items thanks to popover (the compact version, that was already existing on macOS, and which has been added to iOS with iOS 16.4). The popover displays controls to edit the item values. In my case, the popover is more complex than in the following example (reason why I don't include these controls directly into the list), but I simplified as much as possible the example to isolate the issue.

After some tests, the issue only occurs if:

  • The edited value is displayed directly by the item list (if the item does not display the value, the issue is not here)
  • The item modified is entirely hidden by the keyboard (when the keyboard is shown)
  • The test is done on real device (the issue does not occurs on SwiftUI preview, nor on simulator)

To raise the issue:

  1. The value is changed by the user a first time (clic on one of the last items, to open the popover, clic the textField, set a value)
  2. then the user close the popover (clic outside)
  3. then he come back to edit the save item value (clic again on the same item)
  4. and he click the text field --> the popover close itself.

Here is the code sample I did to isolate the issue:

struct Test: View {
    var body: some View {
        List {
            ItemWithPopover()
            ItemWithPopover()
            ItemWithPopover()
            ItemWithPopover()
            ItemWithPopover()
            ItemWithPopover()
            ItemWithPopover()
            ItemWithPopover()
            ItemWithPopover()
        }
    }
}

struct ItemWithPopover: View {
    @State var show = false
    @State var value: Int = 0
    
    var body: some View {
        Button("button (\(value))") {
            show = true
        }
        .padding()
        .popover(isPresented: $show) {
            PopoverContent(value: $value)
                .presentationCompactAdaptation(.popover)
        }
    }
}

struct PopoverContent: View {
    @Binding var value: Int
    
    var body: some View {
        HStack {
            Text("Value: ")
            TextField("Value", value: $value, formatter: NumberFormatter())
                .keyboardType(.numberPad)
        }
        .frame(minWidth: 350)
        .padding()
    }
}

I tried to change the keyboard type, to add a section at the bottom with a size depending on the keyboard size, ... but found nothing working. Could someone help me find the source of the issue and a workaround ? I don't understand why the issue occurs only when the user did a first change...

Note: there is another similar issue when using the resignFirstResponder workaround to close the keyboard when clicking out of the textfield. If the popover anchor is hidden by the keyboard, closing this keyboard by clicking on the text "Values: ..." close also the popover.

0

There are 0 best solutions below