我有一个问题,建议在哪里设置我已经子类化的UITextField
的颜色。这是一些代码。
@IBDesignable
class TextField: UITextField {
//var inset is not important for this question.
@IBInspectable var inset: CGFloat = 10
override init(frame: CGRect) {
super.init(frame: frame)
//tried setting color here, didn't show up in storyboard
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//tried setting color here, didn't show up in storyboard
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
//tried setting color here, didn't show up in storyboard
}
override func textRectForBounds(bounds: CGRect) -> CGRect {
self.backgroundColor = UIColor.redColor()
//HERE IT WORKS!!!
return CGRectInset(bounds, inset, inset)
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return textRectForBounds(bounds)
}
}
所以,我基本上要做的是编写一个UITextField
的子类,用于设置故事板中显示的文本字段(以及将来的其他内容)的背景颜色。我可以让它工作的唯一地方是textRectForBounds:
函数,我必须为文本设置填充。我的问题是,建议在哪里设置UITextField
的背景颜色,并在故事板中显示更改?感谢。