SwiftUI: Add ClearButton to TextField

前端 未结 5 590
长发绾君心
长发绾君心 2021-02-07 22:46

I am trying to add a ClearButton to TextField in SwiftUI when the particular TextField is selected.

The closest I got was creating a ClearButton ViewM

5条回答
  •  死守一世寂寞
    2021-02-07 22:54

    Use .appearance() to activate the button

    var body: some View {
        UITextField.appearance().clearButtonMode = .whileEditing
        return TextField(...)
    }
    

    For reuse try with this:

    func TextFieldUIKit(text: Binding) -> some View{
        UITextField.appearance().clearButtonMode = .whileEditing
        return TextField("Nombre", text: text)
    }
    

提交回复
热议问题