Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当我开始键入蓝色时,我想将文本字段的底层颜色更改为蓝色_Ios_Swift_Viewdidlayoutsubviews - Fatal编程技术网

Ios 当我开始键入蓝色时,我想将文本字段的底层颜色更改为蓝色

Ios 当我开始键入蓝色时,我想将文本字段的底层颜色更改为蓝色,ios,swift,viewdidlayoutsubviews,Ios,Swift,Viewdidlayoutsubviews,何时使用此功能在视图中显示: 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.backg

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

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)
    }
输出:

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

输出:


有人能帮上忙吗?

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

当您在文本字段中写入时,每次都会调用
viewdilayoutsubviews
,并调用designTextField(designTextField使用灰色添加新的子层)。您可以检查是否是第一次使用标志,或者调用ViewWillDisplay或ViewDidDisplay中的designTextField,或者检查是否添加了图层。

这些都是相关代码吗?您确定没有在其他任何地方更改nameLayer吗?因为你的代码看起来不错。是的,这就是所有的代码。谢谢KevinosaurioHappy的帮助。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。
override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        //Text Field Design
        self.designTextField()

        self.view.layoutIfNeeded()
    }
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
        }
    }