Created
March 8, 2019 16:08
-
-
Save nekonora/469af8db97f4d94f28e632adf94cdcd3 to your computer and use it in GitHub Desktop.
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
import UIKit | |
class ModularView: UIView { | |
// MARK: - Meta properties | |
let xibName = "ModularView" | |
// MARK: - IBOutlets | |
// Connect IBOutlets from .xib | |
// MARK: - Properties | |
// Other properties | |
// MARK: - Lifecycle methods | |
// This loads the view for a progamatic call | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
commonInit() | |
} | |
// This loads the view in IB | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
commonInit() | |
} | |
// MARK: - Table view behaviour | |
// MARK: - Initializer | |
private func commonInit() { | |
let bundle = Bundle(for: TKTagsTableView.self) | |
bundle.loadNibNamed(xibName, owner: self, options: nil) | |
// Setup | |
// Do your setup here | |
contentView.setupLayout(self) | |
} | |
} | |
// This sould theoretically be on its own "UIView+.swift" | |
extension UIView { | |
// MARK: - setupLayout | |
func setupLayout(_ container: UIView!) -> Void { | |
self.translatesAutoresizingMaskIntoConstraints = false | |
self.frame = container.frame | |
container.addSubview(self) | |
NSLayoutConstraint( // leading | |
item : self, | |
attribute : .leading, | |
relatedBy : .equal, | |
toItem : container, | |
attribute : .leading, | |
multiplier : 1.0, | |
constant : 0 | |
).isActive = true | |
NSLayoutConstraint( // trailing | |
item : self, | |
attribute : .trailing, | |
relatedBy : .equal, | |
toItem : container, | |
attribute : .trailing, | |
multiplier : 1.0, | |
constant : 0 | |
).isActive = true | |
NSLayoutConstraint( // top | |
item : self, | |
attribute : .top, | |
relatedBy : .equal, | |
toItem : container, | |
attribute : .top, | |
multiplier : 1.0, | |
constant : 0 | |
).isActive = true | |
NSLayoutConstraint( // bottom | |
item : self, | |
attribute : .bottom, | |
relatedBy : .equal, | |
toItem : container, | |
attribute : .bottom, | |
multiplier : 1.0, | |
constant : 0) | |
.isActive = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment