BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.