// MARK: -添加自定义清除按钮
extension UITextField {
///给UITextField添加一个清除按钮
func setModifyClearButton() {
let clearButton = UIButton(type: .custom)
clearButton.setImage(UIImage(named: "textfield_clear_btn"), for: .normal)
clearButton.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
clearButton.contentMode = .scaleAspectFit
clearButton.addTarget(self, action: #selector(UITextField.clear(sender:)), for: .touchUpInside)
self.rightView = clearButton
self.rightViewMode = .whileEditing
}
/// 点击清除按钮,清空内容
@objc func clear(sender: AnyObject) {
self.text = ""
}
}
网友评论