我正在尝试在SwiftUI的TextField中添加ClearButton,当特定的TextField被选中时。
最接近的方法是创建一个ClearButton ViewModifier,并使用.modifer()将其添加到TextField中。
唯一的问题是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)
}
}
}
}