Load a UIView from nib in Swift
Now being able to return -> Self
in swift helps simplify this a bit. Last confirmed on Swift 5.
extension UIView {
class func fromNib(named: String? = nil) -> Self {
let name = named ?? "\(Self.self)"
guard
let nib = Bundle.main.loadNibNamed(name, owner: nil, options: nil)
else { fatalError("missing expected nib named: \(name)") }
guard
/// we're using `first` here because compact map chokes compiler on
/// optimized release, so you can't use two views in one nib if you wanted to
/// and are now looking at this
let view = nib.first as? Self
else { fatalError("view of type \(Self.self) not found in \(nib)") }
return view
}
}
If your .xib
file and subclass share the same name, you can use:
let view = CustomView.fromNib()
If you have a custom name, use:
let view = CustomView.fromNib(named: "special-case")
NOTE:
If you're getting the error "view of type YourType not found in.." then you haven't set the view's class in the .xib
file
Select your view in the .xib
file, and press cmd + opt + 4
and in the class
input, enter your class
My contribution:
extension UIView {
class func fromNib<T: UIView>() -> T {
return Bundle(for: T.self).loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
}
}
Then call it like this:
let myCustomView: CustomView = UIView.fromNib()
..or even:
let myCustomView: CustomView = .fromNib()
Original Solution
- I created a XIB and a class named SomeView (used the same name for convenience and readability). I based both on a UIView.
- In the XIB, I changed the "File's Owner" class to SomeView (in the identity inspector).
- I created a UIView outlet in SomeView.swift, linking it to the top level view in the XIB file (named it "view" for convenience). I then added other outlets to other controls in the XIB file as needed.
- in SomeView.swift, I loaded the XIB inside the "init with code" initializer. There is no need to assign anything to "self". As soon as the XIB is loaded, all outlets are connected, including the top level view. The only thing missing, is to add the top view to the view hierarchy:
.
class SomeView: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSBundle.mainBundle().loadNibNamed("SomeView", owner: self, options: nil)
self.addSubview(self.view); // adding the top level view to the view hierarchy
}
...
}
Note that this way I get a class that loads itself from nib. I could then use SomeView as a class whenever UIView could be used in the project (in interface builder or programmatically).
Update - using Swift 3 syntax
Loading a xib in the following extension is written as an instance method, which can then be used by an initializer like the one above:
extension UIView {
@discardableResult // 1
func fromNib<T : UIView>() -> T? { // 2
guard let contentView = Bundle(for: type(of: self)).loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)?.first as? T else { // 3
// xib not loaded, or its top view is of the wrong type
return nil
}
self.addSubview(contentView) // 4
contentView.translatesAutoresizingMaskIntoConstraints = false // 5
contentView.layoutAttachAll(to: self) // 6
return contentView // 7
}
}
- Using a discardable return value since the returned view is mostly of no interest to caller when all outlets are already connected.
- This is a generic method that returns an optional object of type UIView. If it fails to load the view, it returns nil.
- Attempting to load a XIB file with the same name as the current class instance. If that fails, nil is returned.
- Adding the top level view to the view hierarchy.
- This line assumes we're using constraints to layout the view.
- This method adds top, bottom, leading & trailing constraints - attaching the view to "self" on all sides (See: https://stackoverflow.com/a/46279424/2274829 for details)
- Returning the top level view
And the caller method might look like this:
final class SomeView: UIView { // 1.
required init?(coder aDecoder: NSCoder) { // 2 - storyboard initializer
super.init(coder: aDecoder)
fromNib() // 5.
}
init() { // 3 - programmatic initializer
super.init(frame: CGRect.zero) // 4.
fromNib() // 6.
}
// other methods ...
}
- SomeClass is a UIView subclass that loads its content from a SomeClass.xib file. The "final" keyword is optional.
- An initializer for when the view is used in a storyboard (remember to use SomeClass as the custom class of your storyboard view).
- An initializer for when the view is created programmatically (i.e.: "let myView = SomeView()").
- Using an all-zeros frame since this view is laid out using auto-layout. Note that an "init(frame: CGRect) {..}" method is not created independently, since auto-layout is used exclusively in our project.
- & 6. Loading the xib file using the extension.
Credit: Using a generic extension in this solution was inspired by Robert's answer below.
Edit Changing "view" to "contentView" to avoid confusion. Also changed the array subscript to ".first".
Swift 4 - 5.1 Protocol Extensions
public protocol NibInstantiatable {
static func nibName() -> String
}
extension NibInstantiatable {
static func nibName() -> String {
return String(describing: self)
}
}
extension NibInstantiatable where Self: UIView {
static func fromNib() -> Self {
let bundle = Bundle(for: self)
let nib = bundle.loadNibNamed(nibName(), owner: self, options: nil)
return nib!.first as! Self
}
}
Adoption
class MyView: UIView, NibInstantiatable {
}
This implementation assumes that the Nib has the same name as the UIView class. Ex. MyView.xib. You can modify this behavior by implementing nibName() in MyView to return a different name than the default protocol extension implementation.
In the xib the files owner is MyView and the root view class is MyView.
Usage
let view = MyView.fromNib()