Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. import UIKit
  2.  
  3. typealias Constraint = (UIView, UIView) -> NSLayoutConstraint
  4.  
  5. func equal<L, Axis>(_ to: KeyPath<UIView, L>, constant: CGFloat = 0, priority: UILayoutPriority? = nil) -> Constraint where L: NSLayoutAnchor<Axis> {
  6. return equal(to, to, constant: constant, priority: priority)
  7. }
  8.  
  9. func equal<L, Axis>(_ from: KeyPath<UIView, L>, _ to: KeyPath<UIView, L>, constant: CGFloat = 0, priority: UILayoutPriority? = nil) -> Constraint where L: NSLayoutAnchor<Axis> {
  10. return { view1, view2 in
  11. let constraint = view1[keyPath: from].constraint(equalTo: view2[keyPath: to], constant: constant)
  12. if let priority = priority {
  13. constraint.priority = priority
  14. }
  15. return constraint
  16. }
  17. }
  18.  
  19. func equal<L, Axis>(_ keyPath: KeyPath<UIView, L>, to other: NSLayoutAnchor<Axis>, constant: CGFloat = 0, priority: UILayoutPriority? = nil) -> Constraint where L: NSLayoutAnchor<Axis> {
  20. return { view, _ in
  21. let constraint = view[keyPath: keyPath].constraint(equalTo: other, constant: constant)
  22. if let priority = priority {
  23. constraint.priority = priority
  24. }
  25. return constraint
  26. }
  27. }
  28.  
  29. func equal<L>(_ keyPath: KeyPath<UIView, L>, constant: CGFloat, priority: UILayoutPriority? = nil) -> Constraint where L: NSLayoutDimension {
  30. return { view1, _ in
  31. let constraint = view1[keyPath: keyPath].constraint(equalToConstant: constant)
  32. if let priority = priority {
  33. constraint.priority = priority
  34. }
  35. return constraint
  36. }
  37. }
  38.  
  39. extension Array where Element == Constraint {
  40. static func allEdges(margin: CGFloat = 0, priority: UILayoutPriority? = nil) -> [Constraint] {
  41. return horizontalEdges(margin: margin, priority: priority) + verticalEdges(margin: margin, priority: priority)
  42. }
  43.  
  44. static func horizontalEdges(margin: CGFloat = 0, priority: UILayoutPriority? = nil) -> [Constraint] {
  45. return [equal(\.leadingAnchor, constant: margin, priority: priority), equal(\.trailingAnchor, constant: -margin, priority: priority)]
  46. }
  47.  
  48. static func verticalEdges(margin: CGFloat = 0, priority: UILayoutPriority? = nil) -> [Constraint] {
  49. return [equal(\.topAnchor, constant: margin, priority: priority), equal(\.bottomAnchor, constant: -margin, priority: priority)]
  50. }
  51.  
  52. static func center(priority: UILayoutPriority? = nil) -> [Constraint] {
  53. return [equal(\.centerXAnchor, priority: priority), equal(\.centerYAnchor, priority: priority)]
  54. }
  55.  
  56. static func size(_ size: CGSize) -> [Constraint] {
  57. return [equal(\.widthAnchor, constant: size.width), equal(\.heightAnchor, constant: size.height)]
  58. }
  59. }
  60.  
  61. extension UIView {
  62. func addSubview(_ other: UIView, constraints: [Constraint]) {
  63. other.translatesAutoresizingMaskIntoConstraints = false
  64. addSubview(other)
  65. addConstraints(constraints.map { $0(other, self) })
  66. }
  67.  
  68. func update(_ subview: UIView, constraints: [Constraint]) {
  69. let newConstraints: [NSLayoutConstraint] = constraints.map { $0(subview, self) }
  70. let current = self.constraints
  71. newConstraints.forEach { (newConstraint) in
  72. if let constraint = current.first(where: {
  73. if let currentSecondAnchor = $0.secondAnchor,
  74. let newSecondAnchor = newConstraint.secondAnchor {
  75. return $0.firstAnchor == newConstraint.firstAnchor && newSecondAnchor == currentSecondAnchor
  76. } else {
  77. return $0.firstAnchor == newConstraint.firstAnchor
  78. }
  79. }) {
  80. self.removeConstraint(constraint)
  81. }
  82. self.addConstraint(newConstraint)
  83. }
  84. setNeedsUpdateConstraints()
  85. }
  86. }
  87.  
  88. extension UIStackView {
  89. func addArrangedSubview(_ other: UIView, constraints: [Constraint]) {
  90. other.translatesAutoresizingMaskIntoConstraints = false
  91. addArrangedSubview(other)
  92. addConstraints(constraints.map { $0(other, self) })
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement