在 textFields 上创建和删除边框线
Creating and removing border line on textFields
实际上我正在尝试更改每个文本字段的边框颜色(通过增加边框宽度)。
我已经使用 stotyBoard 给出了 textFieldEmailAddress.tag = 1 和 textFieldPassword.tag = 2。
我正在使用 UITextFieldDelegate。
表示我的要求是:-
1. 当我点击一个文本字段时,它的边框颜色应该是深蓝色(宽度 =0.8)
和其他文本字段边框线颜色应为浅蓝色(宽度 = 0.4)
- 如果我们不点击文本字段,它的边框线颜色应该是浅蓝色。(宽度 = 0.4)。
但是当我第一次点击文本字段时它工作正常时我遇到了一个问题,但是当我点击另一个文本字段时,它给出了深蓝色边框颜色。
问题是无论文本字段变成 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
}
}
首先声明一个方法来设置字段的底部边框,然后像这样使用UITextFieldDelegate
的委托方法
在 viewDidLoad
上,首先将这样的细边框分配给两个字段
override func viewDidLoad() {
super.viewDidLoad()
self.setBottomBorder(self.textFieldEmailAddress, width: 0.4)
self.setBottomBorder(self.textFieldPassword, width: 0.4)
self.setBottomBorder(self.passwordBtn, width: 0.4)
}
func setBottomBorder(view: UIView, width: CGFloat) {
let border = CALayer()
border.name = "BottomBorder"
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: view.frame.size.height - width,
width: view.frame.size.width, height: width)
border.borderWidth = width
view.borderStyle = UITextBorderStyle.None
view.layer.addSublayer(border)
view.layer.masksToBounds = true
}
func removeBottomBorder(view: UIView) {
if let sublayers = view.layer.sublayers {
for layer: CALayer in sublayers {
if layer.name == "BottomBorder" {
layer.removeFromSuperlayer()
break
}
}
}
}
func textFieldDidBeginEditing(textField: UITextField) {
self.removeBottomBorder(textField)
self.setBottomBorder(textField, width: 0.8)
if (textFieldPassword == textField) {
self.setBottomBorder(self.passwordBtn, width: 0.8)
}
}
func textFieldDidEndEditing(textField: UITextField) {
self.removeBottomBorder(textField)
self.setBottomBorder(textField, width: 0.4)
if (textFieldPassword == textField) {
self.removeBottomBorder(self.passwordBtn)
self.setBottomBorder(self.passwordBtn, width: 0.4)
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
试试这个。
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
}
实际上我正在尝试更改每个文本字段的边框颜色(通过增加边框宽度)。
我已经使用 stotyBoard 给出了 textFieldEmailAddress.tag = 1 和 textFieldPassword.tag = 2。
我正在使用 UITextFieldDelegate。
表示我的要求是:-
1. 当我点击一个文本字段时,它的边框颜色应该是深蓝色(宽度 =0.8)
和其他文本字段边框线颜色应为浅蓝色(宽度 = 0.4)
- 如果我们不点击文本字段,它的边框线颜色应该是浅蓝色。(宽度 = 0.4)。
但是当我第一次点击文本字段时它工作正常时我遇到了一个问题,但是当我点击另一个文本字段时,它给出了深蓝色边框颜色。
问题是无论文本字段变成 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
}
}
首先声明一个方法来设置字段的底部边框,然后像这样使用UITextFieldDelegate
的委托方法
在 viewDidLoad
上,首先将这样的细边框分配给两个字段
override func viewDidLoad() {
super.viewDidLoad()
self.setBottomBorder(self.textFieldEmailAddress, width: 0.4)
self.setBottomBorder(self.textFieldPassword, width: 0.4)
self.setBottomBorder(self.passwordBtn, width: 0.4)
}
func setBottomBorder(view: UIView, width: CGFloat) {
let border = CALayer()
border.name = "BottomBorder"
border.borderColor = UIColor.blueColor().CGColor
border.frame = CGRect(x: 0, y: view.frame.size.height - width,
width: view.frame.size.width, height: width)
border.borderWidth = width
view.borderStyle = UITextBorderStyle.None
view.layer.addSublayer(border)
view.layer.masksToBounds = true
}
func removeBottomBorder(view: UIView) {
if let sublayers = view.layer.sublayers {
for layer: CALayer in sublayers {
if layer.name == "BottomBorder" {
layer.removeFromSuperlayer()
break
}
}
}
}
func textFieldDidBeginEditing(textField: UITextField) {
self.removeBottomBorder(textField)
self.setBottomBorder(textField, width: 0.8)
if (textFieldPassword == textField) {
self.setBottomBorder(self.passwordBtn, width: 0.8)
}
}
func textFieldDidEndEditing(textField: UITextField) {
self.removeBottomBorder(textField)
self.setBottomBorder(textField, width: 0.4)
if (textFieldPassword == textField) {
self.removeBottomBorder(self.passwordBtn)
self.setBottomBorder(self.passwordBtn, width: 0.4)
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
试试这个。
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
}