SwiftUI 给keyboard加toolbar

功能:想给输入键盘加一个toolbar. 并且在点击时收起键盘 需要用到@FocusState。

struct ContentView: View {
    @State private var name = "Taylor"
    @FocusState private var isFocused: Bool
    var body: some View {
        TextField("Enter your name", text: $name)
            .textFieldStyle(.roundedBorder)
            .focused($isFocused)
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("Done") {
                        print("Done")
                        isFocused = false
                    }
                }
            }
    }
}

hackingwithswift

你可能感兴趣的:(SwiftUI 给keyboard加toolbar)