如何以编程方式快速设置占位符占位符的位置?

时间:2017-11-29 14:06:28

标签: ios swift textfield

我正在尝试创建以下注册表单。我无法在文本域图像和占位符文本之间设置空格。我尝试了以下内容:

    public ClassName
    String static list = "";  
    Public ClassName (parameters){
      list += getReferenceVariable();
    }

任何想法我该如何解决这个问题? 谢谢enter image description here

1 个答案:

答案 0 :(得分:0)

你可以这样做,UITextField

的扩展名
extension UITextField {

    func setTextIconAndPlaceholder(icon: UIImage, placeholder: String) {
        let imageView = UIImageView()
        imageView.image = icon
        imageView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        let view = UIView()
        view.frame = CGRect(x: 0, y: 2, width: 25, height: 25)
        view.backgroundColor = UIColor.clear
        view.addSubview(imageView)
        self.leftView = view
        self.leftViewMode = UITextFieldViewMode.always
        self.placeholder = placeholder
    }

}