Created
August 9, 2018 10:17
-
-
Save MaherKSantina/35f8b8851db5091361f04c126029e411 to your computer and use it in GitHub Desktop.
This file contains 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 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