InputAccessoryView with SwiftUI

Hey,

is it possible to add an inputAccessoryView to a SwiftUI View? As we can override the inputAccessoryView field on a UIKit ViewController?

I'm working on a messaging app, which needs to display the message input view all the time and "attach" it to the keyboard ones it's appearing.

If if just have a View at the bottom it's moving up and down with the keyboard automatically but once I use .interactive dismissing on my scrollView my view stays up until the keyboard is fully dismissed, which doesn't look good.

Replies

There's a new API to do this in iOS 15. Note that this is not compatible with previous versions of iOS. You'll also need Xcode 13 beta to build this.

See more at https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-a-toolbar-to-the-keyboard

struct ContentView: View {
    @State private var name = "Taylor"

    var body: some View {
        TextField("Enter your name", text: $name)
            .textFieldStyle(.roundedBorder)
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Button("Click me!") {
                        print("Clicked")
                    }
                }
            }
    }
}

Alternatively, you can wrap a UITextView inside your SwiftUI view using UIViewRepresentable. You can then gain access to inputAccessoryView.