I am trying to add a ClearButton to TextField in SwiftUI when the particular TextField is selected.
我得到的最接近的是创建一个ClearButton ViewModifier并将其添加到TextFieldusing.modifer()
唯一的问题是ClearButton永久性的,TextField取消选择时不会消失
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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) swiftui ×1