Slide 25
Slide 25 text
ᶃ9*#ͰσβΠϯ͢Δ7JFX
extension NibLoadable where Self: UIView {
static var nibName: String {
return String(describing: self)
}
static func loadNib(_ bundle: Bundle? = nil) -> UINib {
let bundle = bundle ?? Bundle.main
return UINib(nibName: nibName, bundle: bundle)
}
func loadSubviewFromNib() {
let nib = Self.loadNib(Bundle(for: type(of: self)))
guard let subview = nib.instantiate(withOwner: self, options: nil).first as? UIView else {
return
}
subviews.forEach { $0.removeFromSuperview() }
addSubview(subview)
setNeedsUpdateConstraints()
}
func fitConstraintsSubview() {
if let contentView = subviews.first {
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: self.topAnchor),
contentView.leftAnchor.constraint(equalTo: self.leftAnchor),
contentView.rightAnchor.constraint(equalTo: self.rightAnchor),
contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
])
}
}
}