iosswiftuicustom-keyboard

Problem with SwiftUI Custom Keyboard in Sheet


It try to set a custom keyboard like this is a sheet. The button doesn't show.

import SwiftUI


struct  ContentView: View {
  @State private var showSheet = false
  
  var body: some View {
    VStack {
      Button("Sheet"){showSheet = true}
        .fullScreenCover(isPresented: $showSheet){
      //.sheet(isPresented: $showSheet){
        EditView()
        }
    }
  }
}

struct EditView: View {
    @State private var name = ""

    var body: some View {
        TextField("Enter your name", text: $name)
            .textFieldStyle(.roundedBorder)
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Button("insert Tom") {
                      print("Clicked")
                      name.append(" Tom")
                    }
                }
            }
    }
}

The same code works outside a sheet. What's wrong? Can you help me spot what's missing?


Solution

  • You need to wrap your SheetView in a NavigationView like

    NavigationView {
                TextField("Enter your name", text: $name)
                    .textFieldStyle(.roundedBorder)
                    .toolbar {
                        ToolbarItemGroup(placement: .keyboard) {
                            Button("insert Tom") {
                                print("Clicked")
                                name.append(" Tom")
                            }
                        }
                    }
            }