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

.toolbar in form repeats itself

Forums > SwiftUI

In iOS 16, I have a form with multiple TextFields (in this example, 3). I need each to offer a Done button in the .toolbar. It seems that if you attach a .toolbar modifier to each field, those keyboards will show 3 Done buttons next to each other. Do it to 4 TextFields, you will have 4 Done buttons.

My question is: is it possible to apply Done to every text field without this repetition?

.toolbar { ToolbarItemGroup(placement: .keyboard) { Spacer() Button("Done") { UIApplication.shared.endEditing() } } }

2      

Without see your code I assuming that the toolbar is attched to each TextField. As below

struct ContentView: View {
    @State private var text1 = ""
    @State private var text2 = ""
    @State private var text3 = ""

    var body: some View {
        Form {
            TextField("Text One", text: $text1)
                .toolbar {
                    ToolbarItemGroup(placement: .keyboard) {
                        Spacer()
                        Button("Done") {
                            /// Do something
                        }
                    }
                }
            TextField("Text Two", text: $text2)
                .toolbar {
                    ToolbarItemGroup(placement: .keyboard) {
                        Spacer()
                        Button("Done") {
                            /// Do something
                        }
                    }
                }
            TextField("Text Three", text: $text3)
                .toolbar {
                    ToolbarItemGroup(placement: .keyboard) {
                        Spacer()
                        Button("Done") {
                            /// Do something
                        }
                    }
                }
        }
        .padding()
    }
}

Move the toolbar to theForm

struct ContentView: View {
    @State private var text1 = ""
    @State private var text2 = ""
    @State private var text3 = ""

    var body: some View {
        Form {
            TextField("Text One", text: $text1)

            TextField("Text Two", text: $text2)

            TextField("Text Three", text: $text3)
        }
        .padding()
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                Spacer()
                Button("Done") {
                    /// Do something
                }
            }
        }
    }
}

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.