Skip to content

Instantly share code, notes, and snippets.

@RustemAqtau
Last active July 6, 2020 06:43
Show Gist options
  • Save RustemAqtau/ff08dddb398a55fd01a7c2e68e73b791 to your computer and use it in GitHub Desktop.
Save RustemAqtau/ff08dddb398a55fd01a7c2e68e73b791 to your computer and use it in GitHub Desktop.
Form view extension(for combining two or more elements to one view)
import UIKit
class ButtonFormView: UIView {
init(label: UILabel, button: UIButton) {
super.init(frame: .zero)
self.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(label)
self.addSubview(button)
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: self.topAnchor),
label.leadingAnchor.constraint(equalTo: self.leadingAnchor)
])
NSLayoutConstraint.activate([
button.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 20),
button.leadingAnchor.constraint(equalTo: self.leadingAnchor),
button.trailingAnchor.constraint(equalTo: self.trailingAnchor),
button.heightAnchor.constraint(equalToConstant: 60)
])
bottomAnchor.constraint(equalTo: button.bottomAnchor).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
...
private func setupConstraints() {
let googleView = ButtonFormView(label: googleLabel, button: googleButton)
let googleView2 = ButtonFormView(label: googleLabel, button: googleButton)
let googleView3 = ButtonFormView(label: googleLabel, button: googleButton)
let stackView = UIStackView(arrangeSubviews: [googleView, googleView2, googleview3])
stackView.translateAutoresizingMask = false
stackView.axis = .vertical
stackView.spacing = 40
view.addSubview(stackView)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment