From 9952ca6d2bd4857bc8aec4c44c5c3dad9ec584b9 Mon Sep 17 00:00:00 2001 From: cheykrym Date: Tue, 7 Oct 2025 04:36:21 +0300 Subject: [PATCH] keyboard fix --- yobble/Views/Tab/ChatsTab.swift | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/yobble/Views/Tab/ChatsTab.swift b/yobble/Views/Tab/ChatsTab.swift index a36c6fe..fd0f9ff 100644 --- a/yobble/Views/Tab/ChatsTab.swift +++ b/yobble/Views/Tab/ChatsTab.swift @@ -6,6 +6,9 @@ // import SwiftUI +#if canImport(UIKit) +import UIKit +#endif struct ChatsTab: View { var currentUserId: String? @@ -125,7 +128,9 @@ struct ChatsTab: View { } } .listStyle(.plain) + .modifier(ScrollDismissesKeyboardModifier()) .simultaneousGesture(searchBarGesture) + .simultaneousGesture(tapToDismissKeyboardGesture) // .safeAreaInset(edge: .top) { // VStack(spacing: 0) { // searchBar @@ -193,6 +198,12 @@ struct ChatsTab: View { } } + private var tapToDismissKeyboardGesture: some Gesture { + TapGesture().onEnded { + dismissKeyboard() + } + } + private var isSearching: Bool { !searchText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } @@ -286,6 +297,24 @@ struct ChatsTab: View { } } +private extension ChatsTab { + func dismissKeyboard() { +#if canImport(UIKit) + UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) +#endif + } +} + +private struct ScrollDismissesKeyboardModifier: ViewModifier { + func body(content: Content) -> some View { + if #available(iOS 16.0, *) { + content.scrollDismissesKeyboard(.interactively) + } else { + content + } + } +} + private struct ChatRowView: View { let chat: PrivateChatListItem let currentUserId: String?