【问题标题】:UITextField hide placeholder text or bring cursor to front when clicked swiftUITextField 快速单击时隐藏占位符文本或将光标置于前面
【发布时间】:2019-08-15 05:09:28
【问题描述】:

是否有最新的快速解决方案可以让 UITextField 占位符消失,或者只要用户在字段中单击而不是在他们开始输入时光标就会被带到前面。我尝试了旧帖子中的一些解决方案,并尝试了下面的代码,但似乎没有达到预期的效果。非常感谢任何帮助。

class LogInViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var userTextField: UITextField! 
// the placeholder text is set in IB, with clear Button set to Appears while editing, and clear when editing begins button checked. 

func textFieldDidBeginEditing(textField: UITextField) {
    userTextField.placeholder = nil
    passwordTextField.placeholder = nil
}
}

这对我有用:

添加 userTextField.delegate = self 来查看确实加载了。 // 谢谢我错过了

 func textFieldDidBeginEditing(textField: UITextField) {

    if userTextField.isFirstResponder() == true {
        userTextField.placeholder = nil
    }
}

【问题讨论】:

    标签: swift uitextfield


    【解决方案1】:

    首先要确保从情节提要或在 viewDidLoad() 中设置文本字段的委托

    把你的代码改成这个

    userTextField.placeholder = ""
    passwordTextField.placeholder = ""
    

    【讨论】:

    • 这行不通,因为@richc 仅在用户点击文本字段时才希望这种行为
    • 感谢@OverD!我错过了 ViewDidLoad 中委托的设置。我将 isFirstResponder 添加到 textFieldDidBeginEditing 中,因此只有一个 textField 被清除。 nil 也可以。
    • 很高兴我能帮上忙......而且我一直认为你不能将 nil 分配给字符串,但我猜你生活和学习?
    【解决方案2】:

    如果您有多个 TextField

    1) 将字符串变量添加到您的类中

    class YourViewController : UIViewController {
    
        var placeHolder = ""
    

    2) 添加 UITextFieldDelegate

    extension YourViewController : UITextFieldDelegate {
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        placeHolder = textField.placeholder ?? ""
        textField.placeholder = ""
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField.placeholder == ""{
        textField.placeholder = placeHolder
        }
    }
    

    【讨论】:

      【解决方案3】:

      在 Swift 3 中

      func textFieldDidBeginEditing(_ textField: UITextField) {
          if loginTextField.isFirstResponder == true {
              loginTextField.placeholder = ""
          }
      }
      

      【讨论】:

        【解决方案4】:

        创建 UITextField 的子类:

        class CustomTextField: UITextField {
        
            private var placeholderBackup: String?
        
            override func becomeFirstResponder() -> Bool {
                placeholderBackup = placeholder
                placeholder = nil
                return super.becomeFirstResponder()
            }
        
            override func resignFirstResponder() -> Bool {
                placeholder = placeholderBackup
                return super.resignFirstResponder()
            }
        }
        

        【讨论】:

          【解决方案5】:

          如果你想要一个不可见的占位符(例如创建你自己的自定义文本字段),你可以重写这个方法:

          public override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
                  return .zero
          }
          

          【讨论】:

            猜你喜欢
            • 2016-02-23
            • 2016-08-27
            • 2011-04-11
            • 2012-01-21
            • 2013-02-07
            • 1970-01-01
            • 1970-01-01
            • 2016-09-14
            • 2018-02-16
            相关资源
            最近更新 更多