Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.56 KB | None | 0 0
  1. //
  2. // UIView+PinAutoLayout.swift
  3. // banxi1988
  4. // @LastModified 2015/06/12
  5. // Created by banxi1988 on 15/5/28.
  6. // Copyright (c) 2015年 banxi1988. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. extension NSLayoutConstraint{
  12. func priorityLow(){
  13. priority = 250 // low
  14. }
  15.  
  16. func priorityMedium(){
  17. priority = 500
  18. }
  19.  
  20. func priorityHight(){
  21. priority = 750
  22. }
  23.  
  24. func priority(priority:UILayoutPriority){
  25. self.priority = priority
  26. }
  27.  
  28. func priorityRequired(){
  29. self.priority = 1000 //UILayoutPriorityRequired
  30. }
  31.  
  32. }
  33.  
  34. extension UIView{
  35.  
  36. func pinTop(padding:CGFloat = 15) -> NSLayoutConstraint{
  37. assert(superview != nil, "NO SUPERVIEW")
  38. let c = NSLayoutConstraint(item: self, attribute: .Top, relatedBy: .Equal, toItem: superview!, attribute: .Top, multiplier: 1.0, constant: padding)
  39. superview?.addConstraint(c)
  40. return c
  41. }
  42.  
  43. func pinLeading(padding:CGFloat = 15) -> NSLayoutConstraint{
  44. assert(superview != nil, "NO SUPERVIEW")
  45. let c = NSLayoutConstraint(item: self, attribute: .Leading, relatedBy: .Equal, toItem: superview!, attribute: .Leading, multiplier: 1.0, constant: padding)
  46. superview?.addConstraint(c)
  47. return c
  48. }
  49.  
  50. func pinBottom(padding:CGFloat = 15) -> NSLayoutConstraint{
  51. assert(superview != nil, "NO SUPERVIEW")
  52. let c = NSLayoutConstraint(item: superview!, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: padding)
  53. superview?.addConstraint(c)
  54. return c
  55. }
  56.  
  57. func pinTrailing(padding:CGFloat = 15) -> NSLayoutConstraint{
  58. assert(superview != nil, "NO SUPERVIEW")
  59. let c = NSLayoutConstraint(item: superview!, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1.0, constant: padding)
  60. superview?.addConstraint(c)
  61. return c
  62. }
  63.  
  64. func pinVertical(padding:CGFloat = 15){
  65. pinTop(padding: padding)
  66. pinBottom(padding: padding)
  67. }
  68.  
  69. func pinHorizontal(padding:CGFloat = 15){
  70. pinLeading(padding: padding)
  71. pinTrailing(padding: padding)
  72. }
  73.  
  74. func pinEdge(padding:UIEdgeInsets){
  75. pinLeading(padding: padding.left)
  76. pinTrailing(padding: padding.right)
  77. pinTop(padding: padding.top)
  78. pinBottom(padding: padding.bottom)
  79. }
  80.  
  81.  
  82. func pinLeadingToSibling(sibling:UIView,withMargin margin:CGFloat = 8) -> NSLayoutConstraint{
  83. assert(superview != nil, "NO SUPERVIEW")
  84. assert(superview == sibling.superview, "NOT SIBLING")
  85.  
  86. let c = NSLayoutConstraint(item: self, attribute: .Leading, relatedBy: .Equal, toItem: sibling, attribute: .Trailing, multiplier: 1.0, constant: margin)
  87. superview?.addConstraint(c)
  88. return c
  89. }
  90.  
  91. func pinTrailingToSibing(sibling:UIView,withMargin margin:CGFloat = 8) -> NSLayoutConstraint{
  92. assert(superview != nil, "NO SUPERVIEW")
  93. assert(superview == sibling.superview, "NOT SIBLING")
  94.  
  95. let c = NSLayoutConstraint(item: self, attribute:.Trailing , relatedBy: .Equal, toItem: sibling, attribute: .Leading, multiplier: 1.0, constant: -margin)
  96. superview?.addConstraint(c)
  97. return c
  98. }
  99.  
  100.  
  101. func pinAboveSibling(sibling:UIView,withMargin margin:CGFloat = 8) -> NSLayoutConstraint{
  102. assert(superview != nil, "NO SUPERVIEW")
  103. assert(superview == sibling.superview, "NOT SIBLING")
  104.  
  105. let c = NSLayoutConstraint(item: self, attribute:.Bottom , relatedBy: .Equal, toItem: sibling, attribute: .Top, multiplier: 1.0, constant: -margin)
  106. superview?.addConstraint(c)
  107. return c
  108. }
  109.  
  110. func pinBelowSibling(sibling:UIView,withMargin margin:CGFloat = 8) -> NSLayoutConstraint{
  111. assert(superview != nil, "NO SUPERVIEW")
  112. assert(superview == sibling.superview, "NOT SIBLING")
  113.  
  114. let c = NSLayoutConstraint(item: self, attribute: .Top, relatedBy: .Equal, toItem: sibling, attribute: .Bottom, multiplier: 1.0, constant: margin)
  115. superview?.addConstraint(c)
  116. return c
  117. }
  118.  
  119. func pinCenterXToSibling(sibling:UIView,withOffset offset:CGFloat = 0) -> NSLayoutConstraint{
  120. assert(superview != nil, "NO SUPERVIEW")
  121. assert(superview == sibling.superview, "NOT SIBLING")
  122.  
  123. let c = NSLayoutConstraint(item: self, attribute: .CenterX, relatedBy: .Equal, toItem: sibling, attribute: .CenterX, multiplier: 1.0, constant: offset)
  124. superview?.addConstraint(c)
  125. return c
  126. }
  127.  
  128. func pinCenterYToSibling(sibling:UIView,withOffset offset:CGFloat = 0) -> NSLayoutConstraint{
  129. assert(superview != nil, "NO SUPERVIEW")
  130. assert(superview == sibling.superview, "NOT SIBLING")
  131.  
  132. let c = NSLayoutConstraint(item: self, attribute: .CenterY, relatedBy: .Equal, toItem: sibling, attribute: .CenterY, multiplier: 1.0, constant: offset)
  133. superview?.addConstraint(c)
  134. return c
  135. }
  136.  
  137. func pinWidthToSibling(sibling:UIView,multiplier:CGFloat = 1.0,constant :CGFloat = 0.0) -> NSLayoutConstraint{
  138. assert(superview != nil, "NO SUPERVIEW")
  139. assert(superview == sibling.superview, "NOT SIBLING")
  140. let c = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .Equal, toItem: sibling, attribute: .Width, multiplier: multiplier, constant: constant)
  141. superview?.addConstraint(c)
  142. return c
  143. }
  144.  
  145. func pinHeightToSibling(sibling:UIView,multiplier:CGFloat = 1.0,constant :CGFloat = 0.0) -> NSLayoutConstraint{
  146. assert(superview != nil, "NO SUPERVIEW")
  147. assert(superview == sibling.superview, "NOT SIBLING")
  148. let c = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: sibling, attribute: .Height, multiplier: multiplier, constant: constant)
  149. superview?.addConstraint(c)
  150. return c
  151. }
  152.  
  153. func pinWidth(width:CGFloat) -> NSLayoutConstraint{
  154. let c = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: width)
  155. superview?.addConstraint(c)
  156. return c
  157. }
  158.  
  159. func pinWidthGreaterThanOrEqual(width:CGFloat) -> NSLayoutConstraint{
  160. let c = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .GreaterThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: width)
  161. superview?.addConstraint(c)
  162. return c
  163. }
  164.  
  165. func pinWidthLessThanOrEqual(width:CGFloat) -> NSLayoutConstraint{
  166. let c = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .LessThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: width)
  167. superview?.addConstraint(c)
  168. return c
  169. }
  170.  
  171. func pinHeight(height:CGFloat) -> NSLayoutConstraint{
  172. let c = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: height)
  173. superview?.addConstraint(c)
  174. return c
  175. }
  176.  
  177. func pinHeightGreaterThanOrEqual(height:CGFloat) -> NSLayoutConstraint{
  178. let c = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .GreaterThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: height)
  179. superview?.addConstraint(c)
  180. return c
  181. }
  182.  
  183. func pinHeightLessThanOrEqual(height:CGFloat) -> NSLayoutConstraint{
  184. let c = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .LessThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: height)
  185. superview?.addConstraint(c)
  186. return c
  187. }
  188.  
  189. func pinCenterY(offset:CGFloat=0) -> NSLayoutConstraint{
  190. assert(superview != nil, "NO SUPERVIEW")
  191. let c = NSLayoutConstraint(item: self, attribute: .CenterY, relatedBy: .Equal, toItem: superview!, attribute: .CenterY, multiplier: 1.0, constant: offset)
  192. superview?.addConstraint(c)
  193. return c
  194. }
  195.  
  196. func pinCenterX(offset:CGFloat=0) -> NSLayoutConstraint{
  197. assert(superview != nil, "NO SUPERVIEW")
  198. let c = NSLayoutConstraint(item: self, attribute: .CenterX, relatedBy: .Equal, toItem: superview!, attribute: .CenterX, multiplier: 1.0, constant: offset)
  199. superview?.addConstraint(c)
  200. return c
  201. }
  202.  
  203. func pinAspectRatio(aspectRatio:CGFloat=0) -> NSLayoutConstraint{
  204. let c = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier:aspectRatio, constant: 0)
  205. self.addConstraint(c)
  206. return c
  207. }
  208.  
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement