Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI iOS 15 Keyboard toolbar doesn't show for textfield

Tags:

swiftui

I'm pretty new to SwiftUI, trying to teach myself a few things here and there. But there's this one issue that's been eating at me for a while... and I can't figure out why the toolbar doesn't work/show for me.

The sample code is below, but the button doesn't show nor is there an actual bar. I have iOS 15.2, with XCode 13.2 beta.

TextField("placeholder", text: $text)
.toolbar {
   ToolbarItemGroup(placement: .keyboard) {
       HStack {
           Button(action: {
               hideKeyboard()
           }) {
               Text("Done")
           }
       }
   }
}

EDIT:

Figured out the reason why... it just wouldn't work in a scroll view for some reason. Anyone know why?

like image 949
Kevin Vu Avatar asked Oct 12 '25 10:10

Kevin Vu


1 Answers

Under iOS 15.2 - Xcode 13.2 (not beta).

struct ContentView: View {
    @State var text: String = ""
    
    var body: some View {
        NavigationView {
            VStack {
                Text(text)
                TextField("Enter your name", text: $text)
            }
            .padding()
            .navigationTitle("SwiftUI")
            .toolbar {
//                ToolbarItem(placement: .keyboard) {
//                    Button("Ok") {
//                        print("ok")
//                    }
//                }
                ToolbarItemGroup(placement: .keyboard) {
                    HStack {
                        Button("Press Me") {
                            print("Pressed")
                        }
                        Spacer()
                        Button(action: {
                            print("done")
                        }) { Text("Done") }
                    }
                }
            }
        }
    }
}

Make sure you put everything in a VStack or similar. keyboard

like image 135
Joannes Avatar answered Oct 16 '25 07:10

Joannes