Ive made a custom xib that I've used in my storyboard before and i want simply create an instance of the custom view adjust size and then add it as a subview to a uiscrollview. Ive tried using this block of code in the viewdidload func of my view controller
let cardView = CardView(coder: NSCoder())
cardView!.frame.size.width = 100
cardView!.frame.size.height = 100
scrollView.addSubview(cardView!)
but I'm getting this error
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -containsValueForKey: cannot be sent to an abstract object
of class NSCoder: Create a concrete instance!'
EDIT: this is the code for the swift file connected to CardView.xib
import UIKit
class CardView: UIView {
@IBOutlet var view: UIView!
@IBOutlet weak var cornerView: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSBundle.mainBundle().loadNibNamed("CardView", owner: self, options: nil)
self.addSubview(view)
view.frame = self.bounds
cornerView.layer.cornerRadius = 3
cornerView.layer.masksToBounds = true
view.layer.shadowOffset = CGSizeMake(1, 5);
view.layer.shadowRadius = 2;
view.layer.shadowOpacity = 0.2;
view.layer.masksToBounds = false
}
}
instead of using auto layout i tried simply settings height and width to test adding subviews manually from these 2 lines(also just a heads up i am new to iOS development)
cardView!.frame.size.width = 100
cardView!.frame.size.height = 100
self.view = NSBundle.mainBundle().loadNibNamed("CardView", owner: nil, options: nil)[0] as! UIView
and try once – Audreyaudri