Ios 在interfaceBuilder中添加子视图
我正在尝试向UIView添加一个标签,我想在InterfaceBuilder中预览它,但它似乎不起作用。我不仅看不到标签,而且它也不会将标签附加到视图层次结构中Ios 在interfaceBuilder中添加子视图,ios,swift,Ios,Swift,我正在尝试向UIView添加一个标签,我想在InterfaceBuilder中预览它,但它似乎不起作用。我不仅看不到标签,而且它也不会将标签附加到视图层次结构中 @IBDesignable class UIFloatingLabelInput: UIView { /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance du
@IBDesignable
class UIFloatingLabelInput: UIView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
let Label : UILabel?
func AddLabel(){
Label?.text = "Hello World"
Label?.center = CGPoint(x: self.center.x, y: self.center.y)
self.addSubview(Label!)
}
override init(frame: CGRect){
//Init vars
Label = UILabel(frame: frame)
//Call Initializer
super.init(frame: frame)
AddLabel()
}
required init?(coder: NSCoder){
//Init vars
Label = UILabel(coder: coder)
//CallInitializer
super.init(coder: coder)
AddLabel()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
AddLabel()
}
}
但它在IB中不起作用,但在设备上可以正常工作。您需要提供标签框,使其在IB中起作用
required init?(coder: NSCoder){
//Init vars
Label = UILabel(coder: coder)
//CallInitializer
super.init(coder: coder)
//Give your label a frame
Label.frame = bounds
AddLabel()
}
在调用super.init之前不能使用边界,因为它未设置。但我明白你的意思。