我想重新使用我的屏幕中所有文本字段的代码,我已经为单个文本字段编写了函数,如何将其应用于所有文本字段如何自定义它以便只有单个代码将适用于所有人:代码在这里:
func textfieldborder()
{
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: CGFloat(0), y: textname.frame.size.height - width, width: textname.frame.size.width, height: textname.frame.size.height)
border.borderWidth = width
textname.layer.addSublayer(border)
textname.layer.masksToBounds = true
}
答案 0 :(得分:0)
您应该让您的函数接受UITextField
类型的参数。
func textfieldborder(_ textField: UITextField) {
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.darkGray.cgColor
border.frame = CGRect(x: CGFloat(0), y: textField.frame.size.height - width, width: textField.frame.size.width, height: textField.frame.size.height)
border.borderWidth = width
textField.layer.addSublayer(border) // note that you are setting properties of textField the parameter now!
textField.layer.masksToBounds = true
}
您可以在想要边框的所有文本字段上调用此方法:
textfieldBorder(textname)
textfieldBorder(textFoo)
textfieldBorder(textBar)