Skip to content

Instantly share code, notes, and snippets.

@aoenth
Created August 4, 2021 23:31
Show Gist options
  • Save aoenth/a3f65ed35ada5f8eebdd89a71b3a714b to your computer and use it in GitHub Desktop.
Save aoenth/a3f65ed35ada5f8eebdd89a71b3a714b to your computer and use it in GitHub Desktop.
import UIKit
class TestViewController: UIViewController {
let stackView: UIStackView = {
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = 16
return stackView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupUI()
}
func addView(withColor color: UIColor, toLayoutGuide layoutGuide: KeyPath<UIView, UILayoutGuide>) {
let aView = UIView()
aView.backgroundColor = color.withAlphaComponent(0.5)
aView.layer.borderColor = color.cgColor
aView.layer.borderWidth = 3
aView.translatesAutoresizingMaskIntoConstraints = false
view.insertSubview(aView, belowSubview: stackView)
constrainToLayoutMarginGuide(aView: aView, view: view, layoutGuide: layoutGuide)
addLabel(rect: view[keyPath: layoutGuide].layoutFrame, color: color)
}
func constrainToLayoutMarginGuide(aView: UIView, view: UIView, layoutGuide: KeyPath<UIView, UILayoutGuide>) {
NSLayoutConstraint.activate([
aView.topAnchor.constraint(equalTo: view[keyPath: layoutGuide].topAnchor),
aView.leadingAnchor.constraint(equalTo: view[keyPath: layoutGuide].leadingAnchor),
aView.trailingAnchor.constraint(equalTo: view[keyPath: layoutGuide].trailingAnchor),
aView.bottomAnchor.constraint(equalTo: view[keyPath: layoutGuide].bottomAnchor),
])
}
override func viewDidLayoutSubviews() {
view.subviews.forEach {
$0.removeFromSuperview()
}
stackView.arrangedSubviews.forEach {
stackView.removeArrangedSubview($0)
$0.removeFromSuperview()
}
setupUI()
}
func setupUI() {
view.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
addView(withColor: .systemBlue, toLayoutGuide: \.layoutMarginsGuide)
addView(withColor: .systemGreen, toLayoutGuide: \.readableContentGuide)
addView(withColor: .systemOrange, toLayoutGuide: \.safeAreaLayoutGuide)
}
func addLabel(rect: CGRect, color: UIColor) {
let label = UILabel()
label.font = .monospacedSystemFont(ofSize: 30, weight: .light)
label.text = showCGRect(rect)
label.numberOfLines = 2
label.backgroundColor = color
label.layer.borderColor = UIColor.white.cgColor
label.layer.borderWidth = 0.5
stackView.addArrangedSubview(label)
}
func showCGRect(_ cgRect: CGRect) -> String {
let sizeClass = traitCollection.horizontalSizeClass
return "x: \(cgRect.minX, sizeClass: sizeClass), w: \(cgRect.width, sizeClass: sizeClass)\ny: \(cgRect.minY, sizeClass: sizeClass), h: \(cgRect.height, sizeClass: sizeClass)"
}
}
extension String.StringInterpolation {
mutating func appendInterpolation(_ value: CGFloat, sizeClass: UIUserInterfaceSizeClass) {
let decimals = sizeClass == .regular ? 4 : 3
let string = String(format: "%\(decimals).0f", value)
appendLiteral(string)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment