我有 XIB 文件,其中有 UIView 类型的类,我不会在根 ViewController 中加载该 XIB:
导入 UIKit
类视图控制器:UIViewController {
// MARK: - Variables
// MARK: - IBOutlets
@IBOutlet private weak var mainStackView: UIStackView!
// MARK: - LifeCycle Methods
override func viewDidLoad() {
super.viewDidLoad()
if let view = CustomView.instanceFromNib() {
let view = CustomView.instanceFromNib() as! CustomView
mainStackView.addArrangedSubview(view)
mainStackView.addArrangedSubview(view)
mainStackView.addArrangedSubview(view)
}
}
}
导入基础导入 UIKit
类自定义视图:UIView {
static let id = "CustomView"
@IBOutlet weak var backgroundImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
static func instanceFromNib() -> UIView {
return UINib(nibName: "UserReview", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
此类与键 backgroundImageView 的键值编码不兼容。