i am trying to create a custom UIView in a separate swift file with code like this
import UIKit
class CustomView : UIView {
var contentView:UIView?
// other outlets
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) { // for using CustomView in IB
super.init(coder: aDecoder)
self.commonInit()
fatalError("NSCoding not supported")
}
private func commonInit() {
Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)
guard let content = contentView else { return }
content.frame = self.bounds
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(content)
}
}
then i call it in a viewcontroller with code like this
var custom:CustomView! = CustomView()
override func viewDidLoad() {
super.viewDidLoad()
custom.backgroundColor = UIColor.blue
view.addSubview(custom)
}
yet i got an error like this:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle:
(loaded)' with name 'CustomView''
Why do i get this error code? i have look everywhere on youtube, google,even some stackoverflow answer that similar to this. And they all did the same thing as i did but i am the only one that got this error. i dont know what NIB bundle is. and how do make this custom UIView work