Skip to content

Instantly share code, notes, and snippets.

@pd95
Created November 9, 2024 13:46
Show Gist options
  • Save pd95/088dddaa0328113bb545eb396651beee to your computer and use it in GitHub Desktop.
Save pd95/088dddaa0328113bb545eb396651beee to your computer and use it in GitHub Desktop.
Bottom view not moving while interactive keyboard dismissal is ongong
import SwiftUI
struct MessageInput: View {
@State private var text = ""
@FocusState private var isFocused: Bool
var body: some View {
HStack {
TextField("Enter your message…", text: $text)
.textFieldStyle(.roundedBorder)
.padding(.trailing)
.focused($isFocused)
}
.padding(.horizontal)
.padding(.vertical, 8)
}
}
struct ContentView: View {
var colors: [Color] = [.red, .brown, .green, .cyan, .blue, .purple]
var body: some View {
NavigationStack {
ScrollViewReader { scrollView in
VStack(spacing: 0) {
ScrollView {
VStack(spacing: 0) {
ForEach(colors.indices, id: \.self) { colorIndex in
Rectangle()
.fill(colors[colorIndex])
.frame(height: 200)
.id(colorIndex)
}
}
}
.scrollDismissesKeyboard(.interactively) // allow dismissing the keyboard interactively
.navigationTitle("Chat")
.navigationBarTitleDisplayMode(.inline)
.onAppear() {
scrollView.scrollTo(colors.indices.last, anchor: .bottom)
}
MessageInput()
.background(Material.thin)
}
.background(.red)
}
}
}
}
@pd95
Copy link
Author

pd95 commented Nov 9, 2024

I have the following SwiftUI code below to simulate a big list of messages and an input view at the bottom. It uses the interactive keyboard dismissal on ScrollView to ensure that the keyboard can be dragged down by the user.

The annoying problem I currently see: while the interactive dismissal of the keyboard occurs, the input view stays in the middle of the screen and the white background becomes visible.

Anybody an idea how to avoid this? I've also tried putting the MessageInput view into .safeAreaInset(edge: .bottom) ... with the same result... while dragging the keyboard down, the view stays stuck where it was until the gesture ends.

view-not-moving-on-keyboard-dismissal

@mikecole20
Copy link

Did you find a solution?

@pd95
Copy link
Author

pd95 commented Feb 12, 2025

No, sorry. I've just tested it now again... it simply is not interactive enough to keep up with the keyboard dismiss gesture of the user.
According to WWDC23 video Keep up with the keyboard there is a way with UIKit using layout constraints... but they do not go into details for SwiftUI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment