【发布时间】:2019-09-04 18:25:33
【问题描述】:
创建自定义视图时,是否可以将根视图设置为xib?
目前,如果我创建自定义视图,我需要将笔尖添加为子视图。
如果我想更改背景颜色,我将无法再使用 UIView API。
customView.backgroundColor = UIColor.red // Won't work because nib view is covering the "root" view.
那么,有没有可能做到这一点..
class CustomView: UIView {
func commonInit() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: getInheritedClassName(object: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
// Currently doing this..
addSubview(view)
// Can we do this somehow?
self = view // Set self as the xib view
}
}
【问题讨论】: