Created
March 9, 2019 12:16
-
-
Save barefeettom/f48f6569100415e0ef1fd530ca39f5b4 to your computer and use it in GitHub Desktop.
Simplified summary of NibReplaceable taken from https://github.com/BareFeetWare/BFWControls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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