您提供的代码工作正常,问题出在其他地方。您应该使用默认的init with frame而不是所需的init,或者使用所需的属性创建您的便利init。
class ThemeTextFieldCollab : UITextField {
override init(frame: CGRect) {
super.init(frame: frame)
self.attributedPlaceholder = NSAttributedString(string: self.placeholder ?? "", attributes: [NSForegroundColorAttributeName : UIColor.gray])
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}