Guest User

Untitled

a guest
Jan 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. import UIKit
  2.  
  3. public extension UIView {
  4.  
  5. func addHeightConstraint(_ height: CGFloat) {
  6. addConstraint(NSLayoutConstraint(item: self,
  7. attribute: .height,
  8. relatedBy: .equal,
  9. toItem: nil,
  10. attribute: .notAnAttribute,
  11. multiplier: 1.0,
  12. constant: height))
  13. }
  14.  
  15. func addHeightWidthRatio( _ ratio: CGFloat) {
  16. addConstraint(NSLayoutConstraint(item: self,
  17. attribute: .height,
  18. relatedBy: .equal,
  19. toItem: self,
  20. attribute: .width,
  21. multiplier: ratio, constant: 0))
  22. }
  23.  
  24. func addWidthConstraint(_ height: CGFloat) {
  25. addConstraint(NSLayoutConstraint(item: self,
  26. attribute: .width,
  27. relatedBy: .equal,
  28. toItem: nil,
  29. attribute: .notAnAttribute,
  30. multiplier: 1.0,
  31. constant: height))
  32. }
  33.  
  34. func centerXAxisToSuperView() {
  35. guard let superview = superview else { return }
  36. superview.addConstraint(NSLayoutConstraint(item: self,
  37. attribute: .centerX,
  38. relatedBy: .equal,
  39. toItem: superview,
  40. attribute: .centerX,
  41. multiplier: 1,
  42. constant: 0))
  43. }
  44.  
  45. func centerYAxisToSuperView() {
  46. guard let superview = superview else { return }
  47. superview.addConstraint(NSLayoutConstraint(item: self,
  48. attribute: .centerY,
  49. relatedBy: .equal,
  50. toItem: superview,
  51. attribute: .centerY,
  52. multiplier: 1,
  53. constant: 0))
  54. }
  55.  
  56. func centerToSuperView() {
  57. centerXAxisToSuperView()
  58. centerYAxisToSuperView()
  59. }
  60.  
  61. func stickEdgesToSuperView(edges: [NSLayoutAttribute] = [.leading, .trailing, .top, .bottom], spaces: [CGFloat] = [0.0, 0.0, 0.0, 0.0]) {
  62. guard let superview = superview, edges.count == spaces.count else { return }
  63. for (index, edge) in edges.enumerated() {
  64. switch edge {
  65. case .top, .leading:
  66. superview.addConstraint(NSLayoutConstraint(item: self,
  67. attribute: edge,
  68. relatedBy: .equal,
  69. toItem: superview,
  70. attribute: edge,
  71. multiplier: 1.0,
  72. constant: spaces[index]))
  73. case .bottom, .trailing:
  74. superview.addConstraint(NSLayoutConstraint(item: superview,
  75. attribute: edge,
  76. relatedBy: .equal,
  77. toItem: self,
  78. attribute: edge,
  79. multiplier: 1.0,
  80. constant: spaces[index]))
  81. default:
  82. break
  83. }
  84. }
  85. }
  86.  
  87. func stickToTopView(topView: UIView, _ space: CGFloat) {
  88. guard let superview = superview else { return }
  89. superview.addConstraint(NSLayoutConstraint(
  90. item: self,
  91. attribute: .top,
  92. relatedBy: .equal,
  93. toItem: topView,
  94. attribute: topView == superview ? .top : .bottom,
  95. multiplier: 1.0,
  96. constant: space))
  97. }
  98.  
  99. func stickToBottomView(bottomView: UIView, _ space: CGFloat) {
  100. guard let superview = superview else { return }
  101. superview.addConstraint(NSLayoutConstraint(
  102. item: bottomView,
  103. attribute: bottomView == superview ? .bottom : .top,
  104. relatedBy: .equal,
  105. toItem: self,
  106. attribute: .bottom,
  107. multiplier: 1.0,
  108. constant: space))
  109. }
  110.  
  111. func stickToLeftView(leftView: UIView, _ space: CGFloat) {
  112. guard let superview = superview else { return }
  113. superview.addConstraint(NSLayoutConstraint(
  114. item: self,
  115. attribute: .leading,
  116. relatedBy: .equal,
  117. toItem: leftView,
  118. attribute: leftView == superview ? .leading : .trailing,
  119. multiplier: 1.0,
  120. constant: space))
  121. }
  122.  
  123. func stickToRightView(rightView: UIView, _ space: CGFloat) {
  124. guard let superview = superview else { return }
  125. superview.addConstraint(NSLayoutConstraint(
  126. item: rightView,
  127. attribute: rightView == superview ? .trailing : .leading,
  128. relatedBy: .equal,
  129. toItem: self,
  130. attribute: .trailing,
  131. multiplier: 1.0,
  132. constant: space))
  133. }
  134. }
Add Comment
Please, Sign In to add comment