清除文本字段后占位符不显示SwiftUI

清除文本字段后占位符不显示SwiftUI,swift,textfield,placeholder,Swift,Textfield,Placeholder,我有两个文本字段 TextField("Username", text: $viewModel.usernameStore){} .padding(.all, 8) .background(Color(red:0.5, green: 0.5, blue: 0.5, opacity: 0.3)) .cornerRadius(4.0) .padding(.bottom, 5) SecureField("Password",

我有两个文本字段

 TextField("Username", text: $viewModel.usernameStore){}
        .padding(.all, 8)
        .background(Color(red:0.5, green: 0.5, blue: 0.5, opacity: 0.3))
        .cornerRadius(4.0)
        .padding(.bottom, 5)


    SecureField("Password", text: $viewModel.passwordStore){}
        .padding(.all, 8)
        .background(Color(red:0.5, green: 0.5, blue: 0.5, opacity: 0.3))
        .cornerRadius(4.0)
        .padding(.bottom, 5)
还有一个清除所有这些数据的函数

    func clearCredentials(){
    $viewModel.usernameStore.wrappedValue = ""
    $viewModel.passwordStore.wrappedValue = ""
 }
当调用函数并清除文本字段时,密码的占位符重新填充,但用户名的占位符不填充,它只会被清除

我假设这与密码是一个安全字段有关。
清除占位符文本后,如何让username重新填充占位符文本?

我认为这是一个SwiftUI错误,因为如果点击其他字段,占位符会再次出现

我还有另一个解决方案(这是苹果清除文本字段的标准方式,因此用户知道如何处理它

看看这个:

struct CustomTextField: UIViewRepresentable {

    class Coordinator: NSObject, UITextFieldDelegate {

        @Binding var text: String

        init(text: Binding<String>) {
            _text = text
        }

        func textFieldDidChangeSelection(_ textField: UITextField) {
            text = textField.text ?? ""
        }
    }

    @State var secure : Bool
    @State var initialText : String
    @Binding var text: String
    @State var placeholder : String

    func makeUIView(context: UIViewRepresentableContext<CustomTextField>) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.placeholder = placeholder
        textField.delegate = context.coordinator
        textField.text = initialText
        textField.clearButtonMode = .always
        textField.isSecureTextEntry = secure
        return textField
    }

    func makeCoordinator() -> CustomTextField.Coordinator {
        return Coordinator(text: $text)
    }

    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomTextField>) {
        uiView.text = text
        uiView.placeholder = placeholder
    }
}

struct ContentView: View {

    @State var username : String = ""
    @State var password : String = ""

    func clearCredentials(){
        username = ""
        password = ""

    }

    var body: some View {
        VStack {

            CustomTextField(secure: false, initialText: "", text: $username, placeholder: "Username")
                .fixedSize()
                .padding(.all, 8)
                .background(Color(red:0.5, green: 0.5, blue: 0.5, opacity: 0.3))
                .cornerRadius(4.0)
                .padding(.bottom, 5)


             CustomTextField(secure: true, initialText: "", text: $password , placeholder: "Password")
                .fixedSize()
                .padding(.all, 8)
                .background(Color(red:0.5, green: 0.5, blue: 0.5, opacity: 0.3))
                .cornerRadius(4.0)
                .padding(.bottom, 5)
            Button(action: {
                self.clearCredentials()
            }) {
                Text("Clear credentials")
            }

        }.padding()
    }
}
struct CustomTextField:UIViewRepresentable{
类协调器:NSObject,UITextFieldDelegate{
@绑定变量文本:字符串
init(文本:绑定){
_文本=文本
}
func textField didchangeSelection(textField:UITextField){
text=textField.text??“”
}
}
@状态变量安全:Bool
@状态变量initialText:字符串
@绑定变量文本:字符串
@状态变量占位符:字符串
func makeUIView(上下文:UIViewRepresentableContext)->UITextField{
设textField=UITextField(帧:.0)
textField.placeholder=占位符
textField.delegate=context.coordinator
textField.text=初始文本
textField.clearButtonMode=.always
textField.issecurettentry=secure
返回文本字段
}
func makeCoordinator()->CustomTextField.Coordinator{
退货协调员(文本:$text)
}
func updateUIView(uiView:UITextField,context:UIViewRepresentableContext){
uiView.text=文本
uiView.placeholder=占位符
}
}
结构ContentView:View{
@状态变量username:String=“”
@状态变量密码:String=“”
func clearCredentials(){
username=“”
password=“”
}
var body:一些观点{
VStack{
CustomTextField(安全:false,initialText:,text:$username,占位符:“username”)
.fixedSize()
.padding(.all,8)
.背景(颜色(红色:0.5,绿色:0.5,蓝色:0.5,不透明度:0.3))
.转弯半径(4.0)
.padding(.bottom,5)
CustomTextField(安全:true,initialText:,text:$password,占位符:“password”)
.fixedSize()
.padding(.all,8)
.背景(颜色(红色:0.5,绿色:0.5,蓝色:0.5,不透明度:0.3))
.转弯半径(4.0)
.padding(.bottom,5)
按钮(操作:{
self.clearCredentials()
}) {
文本(“清除凭证”)
}
}.padding()
}
}

我认为这是一个SwiftUI错误,因为如果点击其他字段,占位符会再次出现

我还有另一个解决方案(这是苹果清除文本字段的标准方式,因此用户知道如何处理它

看看这个:

struct CustomTextField: UIViewRepresentable {

    class Coordinator: NSObject, UITextFieldDelegate {

        @Binding var text: String

        init(text: Binding<String>) {
            _text = text
        }

        func textFieldDidChangeSelection(_ textField: UITextField) {
            text = textField.text ?? ""
        }
    }

    @State var secure : Bool
    @State var initialText : String
    @Binding var text: String
    @State var placeholder : String

    func makeUIView(context: UIViewRepresentableContext<CustomTextField>) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.placeholder = placeholder
        textField.delegate = context.coordinator
        textField.text = initialText
        textField.clearButtonMode = .always
        textField.isSecureTextEntry = secure
        return textField
    }

    func makeCoordinator() -> CustomTextField.Coordinator {
        return Coordinator(text: $text)
    }

    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<CustomTextField>) {
        uiView.text = text
        uiView.placeholder = placeholder
    }
}

struct ContentView: View {

    @State var username : String = ""
    @State var password : String = ""

    func clearCredentials(){
        username = ""
        password = ""

    }

    var body: some View {
        VStack {

            CustomTextField(secure: false, initialText: "", text: $username, placeholder: "Username")
                .fixedSize()
                .padding(.all, 8)
                .background(Color(red:0.5, green: 0.5, blue: 0.5, opacity: 0.3))
                .cornerRadius(4.0)
                .padding(.bottom, 5)


             CustomTextField(secure: true, initialText: "", text: $password , placeholder: "Password")
                .fixedSize()
                .padding(.all, 8)
                .background(Color(red:0.5, green: 0.5, blue: 0.5, opacity: 0.3))
                .cornerRadius(4.0)
                .padding(.bottom, 5)
            Button(action: {
                self.clearCredentials()
            }) {
                Text("Clear credentials")
            }

        }.padding()
    }
}
struct CustomTextField:UIViewRepresentable{
类协调器:NSObject,UITextFieldDelegate{
@绑定变量文本:字符串
init(文本:绑定){
_文本=文本
}
func textField didchangeSelection(textField:UITextField){
text=textField.text??“”
}
}
@状态变量安全:Bool
@状态变量initialText:字符串
@绑定变量文本:字符串
@状态变量占位符:字符串
func makeUIView(上下文:UIViewRepresentableContext)->UITextField{
设textField=UITextField(帧:.0)
textField.placeholder=占位符
textField.delegate=context.coordinator
textField.text=初始文本
textField.clearButtonMode=.always
textField.issecurettentry=secure
返回文本字段
}
func makeCoordinator()->CustomTextField.Coordinator{
退货协调员(文本:$text)
}
func updateUIView(uiView:UITextField,context:UIViewRepresentableContext){
uiView.text=文本
uiView.placeholder=占位符
}
}
结构ContentView:View{
@状态变量username:String=“”
@状态变量密码:String=“”
func clearCredentials(){
username=“”
password=“”
}
var body:一些观点{
VStack{
CustomTextField(安全:false,initialText:,text:$username,占位符:“username”)
.fixedSize()
.padding(.all,8)
.背景(颜色(红色:0.5,绿色:0.5,蓝色:0.5,不透明度:0.3))
.转弯半径(4.0)
.padding(.bottom,5)
CustomTextField(安全:true,initialText:,text:$password,占位符:“password”)
.fixedSize()
.padding(.all,8)
.背景(颜色(红色:0.5,绿色:0.5,蓝色:0.5,不透明度:0.3))
.转弯半径(4.0)
.padding(.bottom,5)
按钮(操作:{
self.clearCredentials()
}) {
文本(“清除凭证”)
}
}.padding()
}
}

SwiftUI的文本字段支持占位符文本,就像UITextField一样–文本字段为空时显示的灰色文本,提供提示(“输入密码”)或显示一些示例数据

要设置占位符,请将其作为文本字段初始值设定项的一部分传入,如下所示:

struct ContentView: View {
    @State private var emailAddress = ""

    var body: some View {
        TextField("johnnyappleseed@apple.com", text: $emailAddress)
            .textFieldStyle(RoundedBorderTextFieldStyle())
            .padding()
    }
}

SwiftUI的TextField支持占位符文本,就像UITextField一样–文本字段为空时显示在文本字段中的灰色文本,提供提示(“输入密码”)或显示一些示例数据

要设置占位符,请将其作为文本字段初始值设定项的一部分传入,如下所示:

struct ContentView: View {
    @State private var emailAddress = ""

    var body: some View {
        TextField("johnnyappleseed@apple.com", text: $emailAddress)
            .textFieldStyle(RoundedBorderTextFieldStyle())
            .padding()
    }
}

很好,这很好地使用了textfield.Rig中的x(清除)