I\'m relatively new in the XCode/iOS world; I\'ve done some decent sized storyboard based apps, but I didn\'t ever cut me teeth on the whole nib/xib thing. I want to use the sam
A pretty cool and reusable way of doing this Interface Builder and Swift 4:
Create a new class like so:
import Foundation
import UIKit
@IBDesignable class XibView: UIView {
@IBInspectable var xibName: String?
override func awakeFromNib() {
guard let name = self.xibName,
let xib = Bundle.main.loadNibNamed(name, owner: self),
let view = xib.first as? UIView else { return }
self.addSubview(view)
}
}
In your storyboard, add a UIView that will act as the container for the Xib. Give it a class name of XibView
:
In the property inspector of this new XibView
, set the name of your .xib (without the file extension) in the IBInspectable field:
Add a new Xib view to your project, and in the property inspector, set the Xib's "File's Owner" to XibView
(ensure you've only set the "File's Owner" to your custom class, DO NOT subclass the content view, or it will crash), and again, set the IBInspectable field:
One thing to note: This assumes that you're matching the .xib frame to its container. If you do not, or need it to be resizable, you'll need to add in some programmatic constraints or modify the subview's frame to fit. I use snapkit to make things easy:
xibView.snp_makeConstraints(closure: { (make) -> Void in
make.edges.equalTo(self)
})
Bonus points
Allegedly you can use prepareForInterfaceBuilder()
to make these reusable views visible in Interface Builder, but I haven't had much luck. This blog suggests adding a contentView
property, and calling the following:
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
xibSetup()
contentView?.prepareForInterfaceBuilder()
}