1

I am handling keyboard dismiss on tap event with following code, but the picker here is not responding with inline style. While with menu style, it pop up another layer on top of the form, it works well.

import SwiftUI

struct DismissKeyboardOnTap: ViewModifier {
    func body(content: Content) -> some View {
        content
            .onTapGesture {
                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
            }
    }
}

struct Person {
    enum Gender: String, CaseIterable {
        case male, female
    }

    var name: String
    var age: Int
    var gender: Gender
}

struct ContentView: View {
    @State var person: Person
    
    var body: some View {
        Form(content: {
            HStack {
                Text("Name")
                    .frame(alignment: .leading)
                TextField("", text: $person.name)
                    .multilineTextAlignment(.trailing)
            }
            HStack {
                Text("Age")
                    .frame(alignment: .leading)
                TextField("", value: $person.age, formatter: NumberFormatter())
                    .multilineTextAlignment(.trailing)
                    .keyboardType(.decimalPad)
            }
            Picker("Gender", selection: $person.gender) {
                ForEach(Person.Gender.allCases, id: \.self) { gender in
                    Text(gender.rawValue)
                }
            }
            .pickerStyle(.inline)
//            .pickerStyle(.menu)
        })
        .modifier(DismissKeyboardOnTap())
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(person: Person(name: "John", age: 20, gender: .male))
    }
}
Inline Style Menu Style
Inline Menu

It seems the keyboard modifier is blocking the picker with inline style. Need help from you guys, thx.

Will Zhang
  • 11
  • 3
  • The following post solved my issue with a configurable tap gesture. https://stackoverflow.com/questions/65027321/how-to-fix-a-picker-navigation-works-only-when-i-long-press-a-link/65027508#65027508 – Will Zhang May 03 '23 at 10:01

0 Answers0