gpt4 book ai didi

ios - 我想在开始输入时将文本字段颜色的底层更改为蓝色

转载 作者:行者123 更新时间:2023-11-28 15:01:03 24 4
gpt4 key购买 nike

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)
}

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

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

//Text Field Design
self.designTextField()

self.view.layoutIfNeeded()
}

输出:When to hit textFieldDidBeginEditing

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

输出:When i start typing

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
}
}

有人能帮忙吗???

最佳答案

当您在文本字段中写入时,每次都会调用 viewDidLayoutSubviews 并调用 designTextField(designTextField 添加一个带有 grayColor 的新子层)。你可以用flag检查是否是第一次,或者在viewWillAppear或viewDidAppear中调用designTextField,或者检查图层是否被添加。

关于ios - 我想在开始输入时将文本字段颜色的底层更改为蓝色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48906777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com