在我的项目中,我有几个基于 UITextField 和 UIButton 的自定义类文件。
例如,UITextField 类如下所示:
import UIKit
@IBDesignable class RoundedTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
layer.cornerRadius = 25
layer.borderWidth = 2
layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
clipsToBounds = true
textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
// Placeholder colour
attributedPlaceholder = NSAttributedString(string: placeholder!, attributes: [NSAttributedStringKey.foregroundColor: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)])
}
// Placeholder text indent
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 20, dy: 5)
}
// Editing text indent
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 20, dy: 5)
}
}
这是模拟器显示给我的正确版本:
SimScreenshot
这是IB向我展示的:
IBScreenshot
从我的 IB 屏幕截图中可以看出,唯一更新的元素是占位符文本的缩进。类中的其余自定义没有更新。
有什么想法吗?
附:当我在身份检查器中选择一个元素时,“Designables”字段为“最新”。
我尝试过清理、构建和重新启动 Xcode。
谢谢!
斯科特
最佳答案
我在 Reinier 的第二个链接和我之前学习的 Udemy 类(class)的帮助下解决了这个问题。
我在 setupView()
中实现了我的自定义功能。我调用 prepareForInterfaceBuilder()
还有awakeFromNib()
import UIKit
@IBDesignable class RoundedTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
setupView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupView()
}
// Placeholder text indent
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 20, dy: 5)
}
// Editing text indent
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 20, dy: 5)
}
func setupView() {
layer.cornerRadius = 25
layer.borderWidth = 2
layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
clipsToBounds = true
textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
// Placeholder colour
attributedPlaceholder = NSAttributedString(string: placeholder!, attributes: [NSAttributedStringKey.foregroundColor: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)])
}
}
关于ios - xcode Interface Builder 未更新 IBDesignable 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471742/