Created
July 10, 2021 03:19
-
-
Save froggomad/ee2204765835f2d80644368d4a572518 to your computer and use it in GitHub Desktop.
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 ViewControllerView: UIView { | |
private lazy var buttonStack: UIStackView = { | |
let stack = UIStackView(arrangedSubviews: [viewButton, alertButton]) | |
stack.translatesAutoresizingMaskIntoConstraints = false | |
stack.spacing = 12 | |
stack.axis = .vertical | |
return stack | |
}() | |
lazy var viewButton: UIButton = makeButton(with: "Change backgroundColor") | |
lazy var alertButton = makeButton(with: "Present Alert") | |
init(viewColor: UIColor) { | |
super.init(frame: .zero) | |
subviews() | |
self.backgroundColor = viewColor | |
} | |
required init?(coder: NSCoder) { | |
fatalError("This is a programmatic view") | |
} | |
private func subviews() { | |
addSubview(buttonStack) | |
constraints() | |
} | |
private func constraints() { | |
NSLayoutConstraint.activate([ | |
buttonStack.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -20), | |
buttonStack.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20), | |
buttonStack.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -20) | |
]) | |
} | |
/// Make a button with the given `title`, `backgroundColor`, and `UIButton.ButtonType` | |
private func makeButton(with title: String, backgroundColor: UIColor = .white, type: UIButton.ButtonType = .system) -> UIButton { | |
let button = UIButton(type: type) | |
button.setTitle(title, for: .normal) | |
button.backgroundColor = backgroundColor | |
button.layer.cornerRadius = 10 | |
return button | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment