I'm looking for a way to make nibs that I can then embed and reuse inside a storyboard with all the IBOutlets correctly instantiated. I was able to get it working using a modified version of this tutorial.
The steps for getting this to work are:
- Make a custom view subclass and corresponding nib
- Set
File's Ownerto the custom view subclass - Add a
UIViewas a direct descendent of the nib's view (this is our Content View) - Create an
IBOutletin your view class calledcontentViewof typeUIViewand content the content view in the nib to that outlet In
awakeFromNibdo the following:override func awakeFromNib() { super.awakeFromNib() //call super NSBundle.mainBundle().loadNibNamed("MyView", owner: self, options: nil) self.contentView.translatesAutoresizingMaskIntoConstraints = false self.addSubview(self.contentView) self.addEdgeConstraintWithAttribute(.Left, withSubview: self.contentView) self.addEdgeConstraintWithAttribute(.Top, withSubview: self.contentView) self.addEdgeConstraintWithAttribute(.Right, withSubview: self.contentView) self.addEdgeConstraintWithAttribute(.Bottom, withSubview: self.contentView) }
This process works fine but I don't understand HOW works. And is there a better way of achieving the same thing?