Skip to content

Instantly share code, notes, and snippets.

@MaherKSantina
Created August 9, 2018 10:17
Show Gist options
  • Save MaherKSantina/35f8b8851db5091361f04c126029e411 to your computer and use it in GitHub Desktop.
Save MaherKSantina/35f8b8851db5091361f04c126029e411 to your computer and use it in GitHub Desktop.
class ListingView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initView()
}
func initView() {
// Step 1
guard let xibItems = Bundle.main.loadNibNamed("ListingView", owner: self, options: nil), let firstXibView = xibItems[0] as? UIView else {
fatalError("Xib is empty or first view is not a UIView")
}
// Step 2
firstXibView.translatesAutoresizingMaskIntoConstraints = false
// Step 3
let viewConstraints = [NSLayoutAttribute.top, NSLayoutAttribute.left, NSLayoutAttribute.bottom, NSLayoutAttribute.right].map { (attribute) -> NSLayoutConstraint in
return NSLayoutConstraint(item: firstXibView, attribute: attribute, relatedBy: .equal, toItem: self, attribute: attribute, multiplier: 1, constant: 0)
}
// Step 4
addSubview(firstXibView)
addConstraints(viewConstraints)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment