Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AnselmeKotchap/f5111282b431ecb94102676a20c9c2e1 to your computer and use it in GitHub Desktop.
Save AnselmeKotchap/f5111282b431ecb94102676a20c9c2e1 to your computer and use it in GitHub Desktop.
Initialize Swift subclass of UIView, designed in .xib
// Create CustomView.xib, set File's Owner to CustomView.
// Link the top level view in the XIB to the contentView outlet.
class CustomView : UIView {
@IBOutlet private var contentView:UIView?
// other outlets
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) { // for using CustomView in IB
super.init(coder: aDecoder)
self.commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)
guard let content = contentView else { return }
content.frame = self.bounds
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(content)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment