Xcode: Swift -如何使底线边框颜色出现/消失时,文本字段编辑?



我已经为UITextField添加了一个扩展,以显示带颜色的底部边框。然而,我的意图是在用户点击文本框编辑文本时出现底部边框,并在完成编辑时消失边框。我已经提到了其他关于这个问题的stackoverflow问题,但我找不到一个答案或问题,涉及底部边界和编辑时出现/消失。

extension UITextField {
func addBottomBorder() {
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0,y:self.frame.size.height - 1, width: self.frame.size.width,height: 1)
bottomline.backgroundColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0, alpha: 1.0).cgColor
borderStyle = .none
self.layer.addSublayer(bottomline)
self.layer.masksToBounds = true
}

}

我是swift和Ios开发的新手。如果有人能展示我应该添加什么扩展名文件,以使编辑时边界出现和消失成为可能,那将是有帮助的。

视图控制器

func textFieldDidBeginEditing(_ textField: UITextField) {
movementTextField.addBottomBorder()
notesTextField.addBottomBorder()
descriptionTextField.addBottomBorder()
shotSize.addBottomBorder()
shotNameTextField.addBottomBorder()
}

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
movementTextField.removeBottomBorder()
notesTextField.removeBottomBorder()
descriptionTextField.removeBottomBorder()
shotSize.removeBottomBorder()
shotNameTextField.removeBottomBorder()
return true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
}

扩展更新

extension UITextField {

func addBottomBorder() {
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0,y:self.frame.size.height - 1, width: self.frame.size.width,height: 1)
bottomline.backgroundColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0, alpha: 1.0).cgColor
borderStyle = .none
self.layer.addSublayer(bottomline)
self.layer.masksToBounds = true
}

func removeBottomBorder() {
let removebottomline = CALayer()
removebottomline.frame = CGRect(x: 0,y:self.frame.size.height - 1, width: self.frame.size.width,height: 1)
removebottomline.backgroundColor = UIColor(red: 30.0/255.0, green: 30.0/255.0, blue: 30.0, alpha: 1.0).cgColor
borderStyle = .none
self.layer.addSublayer(removebottomline)
self.layer.masksToBounds = true
}
}

执行以下步骤:

@IBOutlet var nameTextField: UITextField    
@IBOutlet var surnameTextField: UITextField    
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.delegate = self           
}

func textFieldDidBeginEditing(textField: UITextField!) {    //didBegin method(when you start editing in textfield) 
//To addBorder for specific Textfield just make a if else condition like:
if textField == nameTextField {
addBottomBorder()
}

}
func textFieldShouldEndEditing(textField: UITextField!) -> Bool {  //EndEiditing method(when you end editing in textfield) 
//To removeBorder for specific Textfield just make a if else condition like:
if textField == nameTextField {
removeBottomBorder()
}

}
@IBOutlet var textFieldFirst: UITextField   
@IBOutlet var textFieldSecond: UITextField          

override func viewDidLoad() {
super.viewDidLoad()
textFieldFirst = self      
textFieldSecond = self
}


func textFieldDidBeginEditing(textField: UITextField!) {    //didBegin method
if (textFieldFirst = textField)
{
textFieldFirst.addBottomBorder()
}
// marks rest condition in ifelse or have switch condition 

}

func textFieldShouldEndEditing(textField: UITextField!) -> Bool {  //EndEiditing method
if (textFieldFirst = textField)
{
textFieldFirst.removeBottomBorder()
}
// marks rest condition in ifelse or have switch condition 

}

func textFieldShouldReturn(textField: UITextField!) -> Bool {   
textField.resignFirstResponder()

}

最新更新