SwiftUI Keyboard Observer

main

cgaaf/SwiftUI-Keyboard-Observer

SwiftUI Keyboard Observer

This package adds a view extension which adds an iOS software keyboard observer to the environment.

More documentation and updates to come. This is a very early draft. I appreciate any feedback or contributions!

import SwiftUI
import SwiftUI_Keyboard_Observer

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .observeSoftwareKeyboard()
        }
    }
}

struct ContentView: View {
    @State private var text: String = ""
    @Environment(\.softwareKeyboard) var softwareKeyboard
    
    var body: some View {
        VStack {
            Form {
                TextField("Text Field:", text: $text)
            }
            
            if softwareKeyboard?.isVisible == true {
                Button("Dismiss Keyboard") {
                    softwareKeyboard?.dismiss()
                }
                .padding()
            }
        }
    }
}

Description

  • Swift Tools 5.3.0

Dependencies

  • None
Last updated: Thu Jul 15 2021 20:42:33 GMT-0500 (GMT-05:00)