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)
}
}
}
}
@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