Skip to content

Instantly share code, notes, and snippets.

@adiki
Created September 14, 2018 09:25
Show Gist options
  • Save adiki/3eb558af9602d2878b7d0aad0a601d71 to your computer and use it in GitHub Desktop.
Save adiki/3eb558af9602d2878b7d0aad0a601d71 to your computer and use it in GitHub Desktop.
class FeedView: UIView {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: GiftainerLayout())
let tooltipView = TooltipView()
init() {
super.init(frame: .zero)
addSubviews()
}
func addSubviews() {
addSubview(collectionView,
constraints: [pinAllEdges()])
addSubview(tooltipView,
constraints: [pinHorizontalEdges(),
equal(\.topAnchor, \.safeAreaLayoutGuide.topAnchor),
constant(\.heightAnchor, constant: 50)])
}
}
import UIKit
typealias Constraint = (_ child: UIView, _ other: UIView) -> NSLayoutConstraint
func equal<Axis, Anchor>(_ keyPath: KeyPath<UIView, Anchor>, constant: CGFloat = 0, priority: UILayoutPriority = .required) -> [Constraint] where Anchor: NSLayoutAnchor<Axis> {
return equal(keyPath, keyPath, constant: constant, priority: priority)
}
func equal<Axis, Anchor>(_ keyPath: KeyPath<UIView, Anchor>, _ toAnchor: KeyPath<UIView, Anchor>, constant: CGFloat = 0, priority: UILayoutPriority = .required) -> [Constraint] where Anchor: NSLayoutAnchor<Axis> {
return [{ view, other in
let constraint = view[keyPath: keyPath].constraint(equalTo: other[keyPath: toAnchor], constant: constant)
constraint.priority = priority
return constraint
}]
}
func equal<Anchor>(_ keyPath: KeyPath<UIView, Anchor>, multiplier: CGFloat, constant: CGFloat = 0) -> [Constraint] where Anchor: NSLayoutDimension {
return equal(keyPath, keyPath, multiplier: multiplier, constant: constant)
}
func equal<Anchor>(_ keyPath: KeyPath<UIView, Anchor>, _ toAnchor: KeyPath<UIView, Anchor>, multiplier: CGFloat, constant: CGFloat = 0) -> [Constraint] where Anchor: NSLayoutDimension {
return [{ view, other in
view[keyPath: keyPath].constraint(equalTo: other[keyPath: toAnchor], multiplier: multiplier, constant: constant)
}]
}
func equal<Axis, Anchor>(_ keyPath: KeyPath<UIView, Anchor>, to: UIView, constant: CGFloat = 0) -> [Constraint] where Anchor: NSLayoutAnchor<Axis> {
return equal(keyPath, to: to, keyPath, constant: constant)
}
func equal<Axis, Anchor>(_ keyPath: KeyPath<UIView, Anchor>, to: UIView, _ toAnchor: KeyPath<UIView, Anchor>, constant: CGFloat = 0) -> [Constraint] where Anchor: NSLayoutAnchor<Axis> {
return [{ view, _ in
view[keyPath: keyPath].constraint(equalTo: to[keyPath: toAnchor], constant: constant)
}]
}
func constant<Anchor>(_ keyPath: KeyPath<UIView, Anchor>, constant: CGFloat, priority: UILayoutPriority = .required) -> [Constraint] where Anchor: NSLayoutDimension {
return [{ view, _ in
let constraint = view[keyPath: keyPath].constraint(equalToConstant: constant)
constraint.priority = priority
return constraint
}]
}
func pinAllEdges(margin: CGFloat = 0) -> [Constraint] {
return pinHorizontalEdges(margin: margin) + pinVerticalEdges(margin: margin)
}
func pinHorizontalEdges(margin: CGFloat = 0) -> [Constraint] {
return equal(\.leadingAnchor, constant: margin) + equal(\.trailingAnchor, constant: -margin)
}
func pinVerticalEdges(margin: CGFloat = 0) -> [Constraint] {
return equal(\.topAnchor, constant: margin) + equal(\.bottomAnchor, constant: -margin)
}
func pinToCenter() -> [Constraint] {
return equal(\.centerXAnchor) + equal(\.centerYAnchor)
}
func pinToCenter(of view: UIView) -> [Constraint] {
return equal(\.centerXAnchor, to: view) + equal(\.centerYAnchor, to: view)
}
extension UIView {
func addSubview(_ child: UIView, constraints: [[Constraint]]) {
addSubview(child)
child.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(constraints.flatMap { $0.map { $0(child, self) } })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment