Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created February 5, 2023 17:35
Show Gist options
  • Select an option

  • Save foxicode/43ea76651f14d4bfa2359be58be9564e to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/43ea76651f14d4bfa2359be58be9564e to your computer and use it in GitHub Desktop.
SnapKit Sample
import SnapKit
import UIKit
class ViewControllerLayout: UIView {
lazy var square: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
lazy var circle: UIView = {
let view = UIView()
view.backgroundColor = .blue.withAlphaComponent(0.9)
view.layer.cornerRadius = 50
view.layer.masksToBounds = true
return view
}()
init() {
super.init(frame: .zero)
backgroundColor = .white
addSubview(square)
square.snp.makeConstraints {
$0.leading.equalToSuperview().offset(50)
$0.top.equalTo(layoutMarginsGuide.snp.topMargin).offset(100)
$0.trailing.equalToSuperview().offset(-100)
$0.height.equalTo(square.snp.width)
}
addSubview(circle)
circle.snp.makeConstraints {
$0.top.equalTo(layoutMarginsGuide.snp.topMargin).offset(50)
$0.trailing.equalToSuperview().offset(-50)
$0.width.equalTo(100)
$0.height.equalTo(100)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class ViewController: UIViewController {
lazy var layout = ViewControllerLayout()
override func loadView() {
view = layout
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment