NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Reuse Keyboard toolbar code

Forums > SwiftUI

Hi all,

new to the site and finding myself here everytime I google something. I am really new to Swfit and SwiftUI and, after many reads, tutorials and videos I am now on my first playground project.

Anyway... I found this article here, where it is explained how to attach a keyboard accessory view to dismiss it. The code looks easy enough. I remember being more complicated on the Obj-C days...

            TextField(textFieldPlaceholder, text: $textFieldContent).keyboardType(.decimalPad)
                .toolbar {
                    ToolbarItemGroup(placement: .keyboard){
                        Button("Done"){inputIsActive = false}

However, I have the impression that adding this bunch of code for every single textField adds quite a lot of lines. My question is: is there any way to simplify it, aka resuing it? I've been playing around but to no avail...

thanks in advance!


Majid has a nice summary of how to reuse a toolbar:


Thanks a lot @Hatsushira, that seems exactly what I was looking for. I will give it a go this afternoon...


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.