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
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Oct 04 2023 11:56:04 GMT-0900 (Hawaii-Aleutian Daylight Time)