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)
                .focused($inputIsActive)
                .toolbar {
                    ToolbarItemGroup(placement: .keyboard){
                        Spacer()
                        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!

2      

Majid has a nice summary of how to reuse a toolbar: https://swiftwithmajid.com/2020/07/15/mastering-toolbars-in-swiftui/

3      

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

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.