Skip to content

Instantly share code, notes, and snippets.

@crisbit
Last active December 22, 2021 08:34
Show Gist options
  • Save crisbit/31f39b7e1cdd0fe9d29f12a89e0f2eb2 to your computer and use it in GitHub Desktop.
Save crisbit/31f39b7e1cdd0fe9d29f12a89e0f2eb2 to your computer and use it in GitHub Desktop.
How to make a custom reusable view in iOS
import Foundation
import UIKit
class CustomView: UIView {
convenience init() {
// Size doesn't matter if your widget is scalable
// and you are going to use Autolayout constraints
// on it
self.init(frame: CGRectZero)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setupHierarchy()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupHierarchy()
}
func setupHierarchy() {
// Do your setup here (set properties, add subviews, etc.)
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment