Xcode: Swift - 如何在编辑文本字段时使底线边框颜色为 appears/disappear?
Xcode: Swift - How to make bottom line border color that appears/disappear when the text field is edited?
我已经为 UITextField 添加了一个扩展来显示带有颜色的底部边框,这是有效的。但是,我的意图是当用户点击文本字段编辑文本时出现底部边框,并在完成编辑后消失边框。我已经提到了与此相关的其他 Whosebug 问题,但在编辑时找不到涉及底部边框和 appear/disappear 的答案或问题。
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 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()
}
执行以下步骤:
@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()
}
}
我已经为 UITextField 添加了一个扩展来显示带有颜色的底部边框,这是有效的。但是,我的意图是当用户点击文本字段编辑文本时出现底部边框,并在完成编辑后消失边框。我已经提到了与此相关的其他 Whosebug 问题,但在编辑时找不到涉及底部边框和 appear/disappear 的答案或问题。
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 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()
}
执行以下步骤:
@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()
}
}