Skip to content

Instantly share code, notes, and snippets.

@barefeettom
Created March 9, 2019 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barefeettom/f48f6569100415e0ef1fd530ca39f5b4 to your computer and use it in GitHub Desktop.
Save barefeettom/f48f6569100415e0ef1fd530ca39f5b4 to your computer and use it in GitHub Desktop.
Simplified summary of NibReplaceable taken from https://github.com/BareFeetWare/BFWControls
class NibView: UIView, NibReplaceable {
open override func awakeAfter(using coder: NSCoder) -> Any? {
guard subviews.isEmpty,
let nibView = replacedByNibView()
else { return self }
return nibView
}
}
public protocol NibReplaceable where Self: UIView {}
public extension NibReplaceable {
public func replacedByNibView() -> Self? {
let nibView = type(of: self).nibView()
nibView?.copyProperties(from: self)
nibView?.copyConstraints(from: self)
return nibView
}
static func nibView() -> Self? {
guard let nibViews = Bundle(for: self).loadNibNamed(nibName, owner: nil, options: nil),
let nibView = nibViews.first(where: { type(of: $0) == self } ) as? Self
else {
fatalError("\(#function) Could not find an instance of class \(self) in \(nibName) xib")
}
return nibView
}
static var nibName: String {
return String(describing: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment