A simple SwiftUI modifier that automatically moves your view when the keyboard appears. One line. No boilerplate.
Add this package via Swift Package Manager:
https://github.com/tjdrhs90/swiftui-keyboard-avoider.git
Or add it to your Package.swift:
dependencies: [
.package(url: "https://github.com/tjdrhs90/swiftui-keyboard-avoider.git", from: "1.0.0")
]import KeyboardAvoider
struct LoginView: View {
@State private var email = ""
@State private var password = ""
var body: some View {
VStack(spacing: 16) {
TextField("Email", text: $email)
.textFieldStyle(.roundedBorder)
SecureField("Password", text: $password)
.textFieldStyle(.roundedBorder)
Button("Sign In") { }
}
.padding()
.keyboardAvoider() // ← Just add this
}
}That's it. The view automatically adds bottom padding with a smooth animation when the keyboard appears, and removes it when the keyboard hides.
- iOS 14+
- Swift 5.9+
- Xcode 15+
MIT License. See LICENSE for details.