Last active
February 19, 2021 17:32
-
-
Save karigrooms/78cf8016b3d07ffcf2f31e64bae68134 to your computer and use it in GitHub Desktop.
Simple UIView example for Lessons in SwiftUI blog post
This file contains hidden or 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 UIKit | |
class SimpleUIKitView: UIView { | |
lazy var button: ButtonPrimary = { | |
let button = ButtonPrimary() // internal button class | |
button.title = "Primary Button" | |
button.translatesAutoresizingMaskIntoConstraints = false | |
return button | |
}() | |
lazy var heading: LabelHeadingBase = { | |
let label = LabelHeadingBase() // internal label class | |
label.text = "Heading Base" | |
label.textAlignment = .center | |
label.translatesAutoresizingMaskIntoConstraints = false | |
return label | |
}() | |
lazy var stackView: UIStackView = { | |
let view = UIStackView() | |
view.axis = .vertical | |
view.spacing = 8 | |
view.translatesAutoresizingMaskIntoConstraints = false | |
return view | |
}() | |
init() { | |
super.init(frame: .zero) | |
setupView() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private func setupView() { | |
stackView.addArrangedSubview(heading) | |
stackView.addArrangedSubview(button) | |
addSubview(stackView) | |
NSLayoutConstraint.activate([ | |
stackView.centerXAnchor.constraint(equalTo: centerXAnchor), | |
stackView.centerYAnchor.constraint(equalTo: centerYAnchor) | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment