SwiftUI: Add ClearButton to TextField

前端 未结 5 596
长发绾君心
长发绾君心 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:48

    You can add another Binding in your modifier:

    @Binding var visible: Bool
    

    then bind it to opacity of the button:

    .opacity(visible ? 1 : 0)
    

    then add another State for checking textField:

    @State var showClearButton = true
    

    And lastly update the textfield:

    TextField("Some Text", text: $someBinding, onEditingChanged: { editing in
        self.showClearButton = editing
    }, onCommit: {
        self.showClearButton = false
    })
    .modifier( ClearButton(text: $someBinding, visible: $showClearButton))
    
    0 讨论(0)
  • 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<String>) -> some View{
        UITextField.appearance().clearButtonMode = .whileEditing
        return TextField("Nombre", text: text)
    }
    
    0 讨论(0)
  • 2021-02-07 22:55

    Not exactly what you're looking for, but this will let you show/hide the button based on the text contents:

    HStack {
        if !text.isEmpty {
            Button(action: {
                self.text = ""
            }) {
                Image(systemName: "multiply.circle")
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-07 23:06
    public struct ClearButton: ViewModifier {
        @Binding var text: String
    
        public init(text: Binding<String>) {
            self._text = text
        }
    
        public func body(content: Content) -> some View {
            HStack {
                content
                Spacer()
                // onTapGesture is better than a Button here when adding to a form
                Image(systemName: "multiply.circle.fill")
                    .foregroundColor(.secondary) 
                    .opacity(text == "" ? 0 : 1)
                    .onTapGesture { self.text = "" }
            }
        }
    }
    

    Usage:

    @State private var name: String
    
    ...
    
    Form {
        Section() {
            TextField("NAME", text: $name).modifier(ClearButton(text: $name))
        }
    }
    
    0 讨论(0)
  • 2021-02-07 23:14

    Use ZStack to position the clear button appear inside the TextField.

    TextField("Some Text" , text: $someBinding).modifier(ClearButton(text: $someBinding))
    
    struct ClearButton: ViewModifier
    {
        @Binding var text: String
    
        public func body(content: Content) -> some View
        {
            ZStack(alignment: .trailing)
            {
                content
    
                if !text.isEmpty
                {
                    Button(action:
                    {
                        self.text = ""
                    })
                    {
                        Image(systemName: "delete.left")
                            .foregroundColor(Color(UIColor.opaqueSeparator))
                    }
                    .padding(.trailing, 8)
                }
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题