实际上我正在尝试更改每个文本字段的边框颜色(通过增加边框宽度)。
我使用stotyBoard给了textFieldEmailAddress.tag = 1和textFieldPassword.tag = 2.
我正在使用UITextFieldDelegate。
表示我的要求是: -
1.当我点击文本字段时,其边框颜色应为深蓝色(宽度= 0.8)
和其他文本字段边框线颜色应为浅蓝色(宽度= 0.4)
但是我第一次点击文本字段时工作正常我遇到了问题,但是当我点击另一个文本字段时,它会给出深蓝色边框颜色。
问题是,如果textfield变为0.8,它在UI中不会恢复为0.4(意味着UI不更新,只有值更新为0.4)
委托方法或我的下面的代码是否有任何问题: -
//查看加载功能
override func viewDidLoad()
{
//Set delegate
self.textFieldEmailAddress.delegate = self
self.textFieldPassword.delegate = self
//Create border line on text email address fields.
self.borderline(textFieldEmailAddress)
//Create border line on text password fields.
self.borderline(textFieldPassword)
}
func textFieldDidBeginEditing(textField: UITextField)
{
switch textField.tag
{
case 1:
self.Darkborderline(textFieldEmailAddress)
break
case 2:
self.Darkborderline(textFieldPassword)
break
default:
break
}
}
func textFieldDidEndEditing(textField: UITextField)
{
switch textField.tag
{
case 1:
self.borderline(textFieldEmailAddress)
break
case 2:
self.borderline(textFieldPassword)
break
default:
break
}
}
//浅蓝色文本字段上的边框线。
func borderline(textField : UITextField)
{
switch textField.tag
{
case 1:
width = CGFloat(0.40)
let border = CALayer()
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: textFieldEmailAddress.frame.size.height - width,
width: textFieldEmailAddress.frame.size.width, height: width)
border.borderWidth = width
textFieldEmailAddress.borderStyle = UITextBorderStyle.None
textFieldEmailAddress.layer.addSublayer(border)
textFieldEmailAddress.layer.masksToBounds = true
break
case 2:
width = CGFloat(0.40)
let border = CALayer()
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: textFieldPassword.frame.size.height - width,
width: textFieldPassword.frame.size.width, height: width)
border.borderWidth = width
textFieldPassword.borderStyle = UITextBorderStyle.None
textFieldPassword.layer.addSublayer(border)
textFieldPassword.layer.masksToBounds = true
break
default: break
}
}
//文本字段上的深蓝色边框线。
func Darkborderline(textField : UITextField)
{
//let border = CALayer()
switch textField.tag
{
case 1:
width = CGFloat(0.80)
let border = CALayer()
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: textFieldEmailAddress.frame.size.height - width,
width: textFieldEmailAddress.frame.size.width, height: width)
border.borderWidth = width
textFieldEmailAddress.borderStyle = UITextBorderStyle.None
textFieldEmailAddress.layer.addSublayer(border)
textFieldEmailAddress.layer.masksToBounds = true
break
case 2:
width = CGFloat(0.80)
let border = CALayer()
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: textFieldPassword.frame.size.height - width,
width: textFieldPassword.frame.size.width, height: width)
border.borderWidth = width
textFieldPassword.borderStyle = UITextBorderStyle.None
textFieldPassword.layer.addSublayer(border)
textFieldPassword.layer.masksToBounds = true
break
default: break
}
}
答案 0 :(得分:1)
首先声明一个方法将底部边框设置为字段并使用{"result":[{"key":"value"}]}
的委托方法
在UITextFieldDelegate
上,首先将这样的细边框指定给两个字段
viewDidLoad
答案 1 :(得分:1)
尝试一次。
func textFieldShouldBeginEditing(textField: UITextField) -> Bool
{
if 1 == textField.tag
{
selectedTextField = textFieldEmailAddress
self.selectedTextField.layer.borderColor = UIColor.blueColor().CGColor
self.selectedTextField.layer.borderWidth = 0.8
self.selectedTextField = textField
self.textFieldPassword.layer.borderColor = UIColor.blueColor().CGColor
self.textFieldPassword.layer.borderWidth = 0.4
return true
}
else if 2 == textField.tag
{
selectedTextField = textFieldPassword
self.selectedTextField.layer.borderColor = UIColor.blueColor().CGColor
self.selectedTextField.layer.borderWidth = 0.8
self.selectedTextField = textField
self.textFieldEmailAddress.layer.borderColor = UIColor.blueColor().CGColor
self.textFieldEmailAddress.layer.borderWidth = 0.4
return true
}
else
{
return false
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
self.selectedTextField.layer.borderColor = UIColor.blueColor().CGColor
self.selectedTextField.layer.borderWidth = 0.4
self.selectedTextField = UITextField()
textField.resignFirstResponder()
return true
}