0
func designTextField()
    {
        //Set the horizontal line in bottom of text field
        nameLayer.frame = CGRect(x: 0, y: self.tfName.bounds.size.height-1, width: self.tfName.bounds.size.width, height: 1)
        nameLayer.backgroundColor = UIColor.lightGray.cgColor
        tfName.layer.addSublayer(nameLayer)

        //Set the horizontal line in bottom of text field
        phoneLayer.frame = CGRect(x: 0, y: self.tfPhone.bounds.size.height-1, width: self.tfPhone.bounds.size.width, height: 1)
        phoneLayer.backgroundColor = UIColor.lightGray.cgColor
        tfPhone.layer.addSublayer(phoneLayer)

        //Set the horizontal line in bottom of text field
        emailLayer.frame = CGRect(x: 0, y: self.tfEmail.bounds.size.height-1, width: self.tfEmail.bounds.size.width, height: 1)
        emailLayer.backgroundColor = UIColor.lightGray.cgColor
        tfEmail.layer.addSublayer(emailLayer)
    }

何时使用此功能在视图中显示:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        //Text Field Design
        self.designTextField()

        self.view.layoutIfNeeded()
    }

输出 :何时点击 textFieldDidBeginEditing

当点击“名称”文本字段时,底层背景颜色将变为蓝色。但是当我在文本字段中输入一些字符时,文本字段的底部颜色将变为浅灰色(但当我输入时,底层将与蓝色相同。)

输出 :当我开始打字

func textFieldDidBeginEditing(_ textField: UITextField)
    {
        if textField == self.tfName
        {
            nameLayer.backgroundColor = UIColor(red: 11/255, green: 174/255, blue: 250/255, alpha: 1).cgColor
            imgName = UIImage(named: "user2")!
            imgVwName.image = imgName
            tfName.leftView = imgVwName
        }
        else if textField == self.tfPhone
        {
            phoneLayer.backgroundColor = UIColor(red: 11/255, green: 174/255, blue: 250/255, alpha: 1).cgColor
            imgPhone = UIImage(named: "phone2")!
            imgVwPhone.image = imgPhone
            tfPhone.leftView = imgVwPhone
        }
        else if textField == self.tfEmail
        {
            emailLayer.backgroundColor = UIColor(red: 11/255, green: 174/255, blue: 250/255, alpha: 1).cgColor
            imgEmail = UIImage(named: "mail2")!
            imgVwEmail.image = imgEmail
            tfEmail.leftView = imgVwEmail
        }
    }

    func textFieldDidEndEditing(_ textField: UITextField)
    {
        if textField == self.tfName
        {
            nameLayer.backgroundColor = UIColor.lightGray.cgColor
            imgName = UIImage(named: "user1")!
            imgVwName.image = imgName
            tfName.leftView = imgVwName
        }
        else if textField == self.tfPhone
        {
            phoneLayer.backgroundColor = UIColor.lightGray.cgColor
            imgPhone = UIImage(named: "phone1")!
            imgVwPhone.image = imgPhone
            tfPhone.leftView = imgVwPhone
        }
        else if textField == self.tfEmail
        {
            emailLayer.backgroundColor = UIColor.lightGray.cgColor
            imgEmail = UIImage(named: "mail1")!
            imgVwEmail.image = imgEmail
            tfEmail.leftView = imgVwEmail
        }
    }

有人可以帮忙吗???

4

2 回答 2

1

当您在 textfieldviewDidLayoutSubviews中写入时,每次都会调用 designTextField(designTextField 添加一个带有 grayColor 的新子层)。您可以检查是否是第一次使用标志,或者在 viewWillAppear 或 viewDidAppear 中调用 designTextField,或者检查是否添加了图层。

于 2018-02-21T13:52:31.587 回答
0

我建议您使用SkyFloatingField,因为它是满足您要求的轻量级库。

于 2018-02-21T13:52:16.027 回答