UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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: https://swiftwithmajid.com/2020/07/15/mastering-toolbars-in-swiftui/


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 RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.