Last active
December 22, 2021 08:34
-
-
Save crisbit/31f39b7e1cdd0fe9d29f12a89e0f2eb2 to your computer and use it in GitHub Desktop.
How to make a custom reusable view in iOS
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
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