ios - Swift - 以编程方式向 UITextfield 添加清除按钮
问题描述
我在向我的UITextfield
.
这是我的textfield
:
let emailTextField: CustomTextField = {
let v = CustomTextField()
v.borderActiveColor = .white
v.borderInactiveColor = .white
v.textColor = .white
v.font = UIFont(name: "AvenirNext-Regular", size: 17)
v.placeholder = "Email-Adresse"
v.placeholderColor = .gray
v.placeholderFontScale = 1
v.clearButtonMode = UITextField.ViewMode.always
v.minimumFontSize = 13
v.borderStyle = .line
v.autocapitalizationType = .none
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
如您所见,我设置clearButtonMode = .always
了但未显示。
我CustomTextFieldClass
的也没什么特别的:
class CustomTextField: HoshiTextField {
/// the left padding
@IBInspectable public var leftPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }
/// the right padding
@IBInspectable public var rightPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }
/// Text rectangle
///
/// - Parameter bounds: the bounds
/// - Returns: the rectangle
override public func textRect(forBounds bounds: CGRect) -> CGRect {
let originalRect: CGRect = super.editingRect(forBounds: bounds)
return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
}
/// Editing rectangle
///
/// - Parameter bounds: the bounds
/// - Returns: the rectangle
override public func editingRect(forBounds bounds: CGRect) -> CGRect {
let originalRect: CGRect = super.editingRect(forBounds: bounds)
return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
}
有谁知道为什么不显示清除按钮???
解决方案
推荐阅读
- mips - MIPS 多重溢出
- java - 在没有循环的Java中反转整数
- python-3.x - 在 python 3.6 中覆盖 alt-f4 关闭 tkinter 窗口并将其替换为其他内容
- firebase - 上传后 Firebase 存储下载网址不可用
- swift - MLDataTable 中的多个目标列 - Swift 中的 CreateML 框架
- r - 删除使用 plotmath 制作的 ggrepel 标签之间的空间
- vba - 资源管理器模式下的 Outlook 邮件项已旧
- apache - 如何在我的 localhost 服务器上运行 index.html 文件?
- reactjs - 渲染后在元素上添加 onClick 事件的正确方法是什么?
- c# - 在 C# 中,检索已存储在表单对象标签中的文本数据