Untitled
unknown
swift
3 years ago
1.3 kB
9
Indexable
import UIKit
//@IBDesignable
class XibView: UIView {
var viewFromNib: UIView? {
let typeOfSelf = type(of: self)
let name = String(describing: typeOfSelf)
let bundle = Bundle(for: typeOfSelf)
let nib = UINib(nibName: name, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
private func xibSetup() {
guard let view = viewFromNib else { return }
pin(subView: view)
}
}
extension UIView {
func pin(subView: UIView, withInsets insets: UIEdgeInsets = .zero, rectEdge: UIRectEdge = .all) {
subView.translatesAutoresizingMaskIntoConstraints = false
addSubview(subView)
subView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: insets.left).isActive = rectEdge.contains(.left)
subView.topAnchor.constraint(equalTo: topAnchor, constant: insets.top).isActive = rectEdge.contains(.top)
subView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: insets.bottom).isActive = rectEdge.contains(.bottom)
subView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: insets.right).isActive = rectEdge.contains(.right)
}
}
Editor is loading...