0

Problem: If the navigated view has searchable modifier and when you select the searchable field (so keyboard is open) and if you pop this view back by selecting an item, first view is freezes (not respond any user input). This issue only occurs with the searchable field and not with any other text fields. Additionally, the problem only exists in iOS 16, as there are no issues in iOS 15.

Xcode: 14.3
Project Target Version: iOS 15
Simulator & Device Version: iOS 16

Code for reproducing problem:

struct ContentView: View {
    private let options: [String] = ["ABC", "DEF", "GHI", "JKL", "MNO"]
    @State private var selection: String?
    var body: some View {
        NavigationView {
            NavigationLink {
                CustomPicker(selection: $selection, options: options)
            } label: {
                HStack {
                    Text("Picked value")
                    Spacer()
                    Text(selection ?? "Pick >")
                }
                .accentColor(.black)
            }
            .padding()
        }
        .navigationViewStyle(.stack)
    }
}

struct CustomPicker: View {
    @Environment(\.dismiss) private var dismiss
    @Binding var selection: String?
    let options: [String]
    var body: some View {
        List(options, id: \.self) { option in
            Button {
                selection = option
                dismiss()
            } label: {
                Text(option)
            }
        }
        .searchable(text: .constant(""))
    }
}

I tried dismiss view using isActive in NavigationLink(isActive: ) and it does not work.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
eghnacr
  • 129
  • 7

1 Answers1

1

I had the same issue a few months ago: Navigation bug when dismissing view while focussing on empty .searchable() modifier

I made a bug report to apple but have yet to receive any feedback from them.

For now I worked around this bug by setting my .searchable() binding to a space " " before navigating away and this seems to work for me.

You can try something like this:

struct CustomPicker: View {
@Environment(\.dismiss) private var dismiss
@Binding var selection: String?
@State var mySearchText: String // <---
let options: [String]
var body: some View {
    List(options, id: \.self) { option in
        Button {
            selection = option
            mySearchText = " " <---
            dismiss()
        } label: {
            Text(option)
        }
    }
    .searchable(text: $mySearchText) // <---
}

}

Kevin D
  • 128
  • 7