SwiftUI: Add ClearButton to TextField

swiftui textfield
swiftui textfield keystroke
swiftui center textfield
swiftui textfield(search)
swiftui textfield focus
textfieldshouldclear swift
swiftui textfield placeholder
swiftui textfield font

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 ViewModifier and adding it to the TextField using .modifer()

The only problem is ClearButton is permanent and does not disappear when TextField is deselected

TextField("Some Text" , text: $someBinding).modifier(ClearButton(text: $someBinding))

struct ClearButton: ViewModifier {
    @Binding var text: String

    public func body(content: Content) -> some View {
        HStack {
            content
            Button(action: {
                self.text = ""
            }) {
                Image(systemName: "multiply.circle.fill")
                    .foregroundColor(.secondary)
            }
        }
    }
}

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)
            }
        }
    }
}

SwiftUI TextField clearbutton how?, Use ZStack to position the clear button appear inside the TextField . TextField("​Some Text" , text: $someBinding).modifier(ClearButton(text: $someBinding))  I do not know a direct way to do it like with UITextField. There are still a lot of limits to SwiftUI. But you could put the textField in a containes witha clearbutton…

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))
    }
}

SOLVED: TextField - Dismiss keyboard, How can you add the clearbutton to a TextField? struct ClearButton: ViewModifier { @Binding var text: String public func body(content: Content) -> some View  How to Create and Customize a TextField in SwiftUI. UITextField, evolved To customize the border of the TextField, you will be using .overlay, which will add a layer in front of the text field.

Use .appearance() to activate the button

var body: some View {

        UITextField.appearance().clearButtonMode = .whileEditing

        return TextField(...)

}

How to make a clear button appear in a textfield, keyboard when finished with entry (especially when using numberPad or decimalPad) from a textField in SwiftUI? Also to add an clearButton  SwiftUI TextField complete tutorial TextField in SwiftUI is a simple control that shows an editable text interface (equivalent to UITextField in UIKit). Because TextField allows a user to type text, it also needs a way of storing the entered text in a State variable which can then be used to read the input.

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))

How to make a clear button appear in a textfield, How to make a clear button appear in a textfield the standard approach is to add a clear button to the right edge of the text field. in Xcode · How to use Instruments to profile your SwiftUI code and identify slow layouts · How  The onCommit callback is not called for the previously selected TextField. Edit 2: Adding an example for if you want to call a function committed() when a user taps return or changes TextField, and changed() when the user taps the TextField:

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")
        }
    }
}

SwiftUI Hackathon - @RosberryApps, swift,uitextfield,swift 4,textfields,learn,swift 3,clear button in textbox,keyboard swift Duration: 3:26 Posted: Aug 4, 2019 Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. SwiftUI: Add ClearButton to TextField. 1.

Swiftui textfield clear button, Adding NavigationView we get NavigationBar and can easily add a title to the TextField itself, the background, the icon and the Clear button. When initializing a TextField element, two properties are required. The first property is the placeholder of the TextField. The second property is a property wrapper variable, containing the value of the TextField. To create a property wrapper variable in SwiftUI, define a variable and add the text @State before. When referring to the property

SwiftUI TextField Tutorial, I am trying to add a ClearButton to TextField in SwiftUI when the particular TextField is selected. That'll dismiss the keyboard and remove the focus from the text  does anyone know an easy and elegant way of dismissing the keyboard when finished with entry (especially when using numberPad or decimalPad) from a textField in SwiftUI? Also to add an clearButton and once the entry is cleared, that the placeholder will be shown? Thanks a lot in advance for helping me out! Rgds, Martin

Chapter 12, Clear Button for the text field, SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas modifier is added to make it clear where the boundary of the textfield is. Okay, let’s start with the basics and create a simple button using SwiftUI. First, fire up Xcode and create a new project using the Single View Application template. Type the name of the project. I set it to SwiftUIButton but you’re free to use any other name. All you need to ensure is check the Use SwiftUI option.

Comments
  • Why did you unaccept my answer?
  • It's better to replace ZStack with HStack. Otherwise long text will go under the image.
  • I've tried to use this but the bound value doesn't change. Within the ClearButton block, the value of text is the value of the TextField the first time I press it. Then, its indeed cleared, but the cleared text value is not set on the TextField. So, I'm trying to understand why we need the _text attribute. I'm still trying to understand SwiftUI so sorry it its obvious.
  • Thank you! I am using multiple TextField as part of a Form, is there a solution that doesn't involve creating a showClearButton @State for every TextField ?
  • Yes, creating custom View for your custom TextField.