Skip to content

Instantly share code, notes, and snippets.

@rsaenzi
Last active August 29, 2020 11:04
Show Gist options
  • Save rsaenzi/1f5b02729ad42e6ca063e9db7d84408d to your computer and use it in GitHub Desktop.
Save rsaenzi/1f5b02729ad42e6ca063e9db7d84408d to your computer and use it in GitHub Desktop.
Template for custom view loaded from a .xib file
//
// CustomView.swift
//
// Created by Rigoberto Sáenz Imbacuán (https://www.linkedin.com/in/rsaenzi/)
// Copyright © 2019. All rights reserved.
//
// Notes:
// - Bind content to the ContentView in Interface Builder
// - Make the xib's file owner be this class
import UIKit
class CustomView: UIView {
// This property must be bound to the whole view in Interface Builder
@IBOutlet private weak var content: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sharedInit()
}
private func sharedInit() {
loadNib()
addSubview(content)
// Expand to fill its parent
content.frame = self.bounds
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}
@rsaenzi
Copy link
Author

rsaenzi commented Mar 12, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment