Skip to content

Commit 38fe145

Browse files
committed
Fixed keyboard overlap issue i=on the text filed in long form
1 parent d1cae19 commit 38fe145

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

GoInfoGame/GoInfoGame/Helpers/Extensions.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import MapKit
1010
import RealmSwift
1111
import ARKit
1212
import SwiftUI
13+
import Combine
1314

1415

1516
// Extension to check if a polyline intersects with a coordinate
@@ -161,4 +162,27 @@ extension UIApplication {
161162
static func window() -> UIWindow? {
162163
return UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }
163164
}
165+
166+
var safeAreaBottomInset: CGFloat {
167+
guard let windowScene = connectedScenes.first as? UIWindowScene,
168+
let window = windowScene.windows.first(where: \.isKeyWindow) else {
169+
return 0
170+
}
171+
return window.safeAreaInsets.bottom
172+
}
173+
}
174+
175+
extension Publishers {
176+
static var keyboardHeight: AnyPublisher<CGFloat, Never> {
177+
let willShow = NotificationCenter.default
178+
.publisher(for: UIResponder.keyboardWillShowNotification)
179+
.map { ($0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect)?.height ?? 0 }
180+
181+
let willHide = NotificationCenter.default
182+
.publisher(for: UIResponder.keyboardWillHideNotification)
183+
.map { _ in CGFloat(0) }
184+
185+
return MergeMany(willShow, willHide)
186+
.eraseToAnyPublisher()
187+
}
164188
}

GoInfoGame/GoInfoGame/quests/LongQuests/View/LongForm.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import SwiftUI
99
import CoreLocation
10-
10+
import Combine
1111

1212
struct LongForm: View, QuestForm {
1313

@@ -58,6 +58,8 @@ struct LongForm: View, QuestForm {
5858

5959
@StateObject private var noteViewModel = NotesViewModel()
6060

61+
@State private var keyboardHeight: CGFloat = 0
62+
6163
var body: some View {
6264
ZStack {
6365
VStack(alignment: .leading) {
@@ -192,6 +194,13 @@ struct LongForm: View, QuestForm {
192194
Text("No Quests available")
193195
}
194196
}
197+
.padding(.bottom, keyboardHeight)
198+
.onReceive(Publishers.keyboardHeight) { height in
199+
let safeAreaBottom = UIApplication.shared.safeAreaBottomInset
200+
withAnimation {
201+
keyboardHeight = max(0, height - safeAreaBottom)
202+
}
203+
}
195204
}
196205
.hideKeyboardOnTap()
197206

0 commit comments

Comments
 (0)