最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - How to dismiss keyboard when I click on a native Menu item? - Stack Overflow

programmeradmin3浏览0评论

I am encountering the problem that when I click on the native menu it does not pay attention to the gestures or anything else, so I have no way of knowing if it is being clicked or not and thus remove the keyboard, does anyone have an idea how to do it? I have tried onappear with disapear but it never disapear

onappear, disapear, tapgesture, simultaneus gesture import SwiftUI

struct ContentView: View {
    @State private var text: String = ""

    var body: some View {
        NavigationView {
            VStack {
                TextField("Escribe algo...", text: $text)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding()

                Menu {
                    Button("Perfil", action: menuTapped)
                    Button("Configuración", action: menuTapped)
                    Button(role: .destructive, action: menuTapped) {
                        Label("Cerrar sesión", systemImage: "power")
                    }
                } label: {
                    Label("Menú", systemImage: "line.3.horizontal")
                        .font(.title)
                        .padding()
                }
                .onTapGesture {
                    hideKeyboard()
                }
            }
            .navigationTitle("Inicio")
        }
    }

    // Acción al tocar el menú
    private func menuTapped() {
        hideKeyboard()
        print("Opción seleccionada")
    }

    // Método para ocultar el teclado
    private func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

Never enter in ontapgesture

I am encountering the problem that when I click on the native menu it does not pay attention to the gestures or anything else, so I have no way of knowing if it is being clicked or not and thus remove the keyboard, does anyone have an idea how to do it? I have tried onappear with disapear but it never disapear

onappear, disapear, tapgesture, simultaneus gesture import SwiftUI

struct ContentView: View {
    @State private var text: String = ""

    var body: some View {
        NavigationView {
            VStack {
                TextField("Escribe algo...", text: $text)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding()

                Menu {
                    Button("Perfil", action: menuTapped)
                    Button("Configuración", action: menuTapped)
                    Button(role: .destructive, action: menuTapped) {
                        Label("Cerrar sesión", systemImage: "power")
                    }
                } label: {
                    Label("Menú", systemImage: "line.3.horizontal")
                        .font(.title)
                        .padding()
                }
                .onTapGesture {
                    hideKeyboard()
                }
            }
            .navigationTitle("Inicio")
        }
    }

    // Acción al tocar el menú
    private func menuTapped() {
        hideKeyboard()
        print("Opción seleccionada")
    }

    // Método para ocultar el teclado
    private func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

Never enter in ontapgesture

Share Improve this question edited 14 hours ago RaaY asked 15 hours ago RaaYRaaY 11 bronze badge New contributor RaaY is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3
  • 1 Please provide a minimal reproducible example – Benzy Neez Commented 15 hours ago
  • Do you want to dismiss the keyboard whenever anything in menu is clicked @Raay? – Aakarsh Kumar Commented 11 hours ago
  • I want to dismiss the keyboard all the times the menu show up @AakarshKumar but i can only use onappear 1 time because Menu dont have dissapear – RaaY Commented 11 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

Menu does not trigger any gestures in SwitUI. You can use a Bool @State variable to track the changes. Whenever the state is true hide the keyboard and after a small delay reset the state


struct ContentView: View {
    @State private var text: String = ""
    @State private var isMenuPresented = false // Track menu state

    var body: some View {
        NavigationView {
            VStack {

                Menu {
                    // your code
                } label: {
                    Label("Menú", systemImage: "line.3.horizontal")
                        .font(.title)
                        .padding()
                        .simultaneousGesture(TapGesture().onEnded {
                            isMenuPresented = true
                        })
                }
            }
            .navigationTitle("Inicio")
            .onChange(of: isMenuPresented) { newValue in
                 if newValue { 
                     hideKeyboard()
                     resetMenuState()
                 }
             }
        }
    }

    // Acción al tocar el menú
    private func menuTapped() {
        print("Opción seleccionada")
        resetMenuState()
    }

    // Método para ocultar el teclado
    private func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
    
    // Reset menu state after a delay
    private func resetMenuState() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
            isMenuPresented = false
        }
    }
}

发布评论

评论列表(0)

  1. 暂无评论