Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created May 14, 2020 08:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevencurtis/721beb2d2bf198ea2c2ade564d0433fd to your computer and use it in GitHub Desktop.
Save stevencurtis/721beb2d2bf198ea2c2ade564d0433fd to your computer and use it in GitHub Desktop.
BlueView
class BlueView: UIView {
var myLabel = UILabel()
// init from code
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
// init from xib or storyboard
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
// common setup code
private func setupView() {
backgroundColor = .red
myLabel.text = "a"
myLabel.textAlignment = .center
self.addSubview(myLabel)
myLabel.translatesAutoresizingMaskIntoConstraints = false
myLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
myLabel.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
myLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
myLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment