省钱达人

UIView+LayoutMethods.m 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // UIView+LayoutMethods.m
  3. // TmallClient4iOS-Prime
  4. //
  5. // Created by casa on 14/12/8.
  6. // Copyright (c) 2014年 casa. All rights reserved.
  7. //
  8. #import "UIView+LayoutMethods.h"
  9. @implementation UIView (LayoutMethods)
  10. // coordinator getters
  11. - (CGFloat)height
  12. {
  13. return self.frame.size.height;
  14. }
  15. - (CGFloat)width
  16. {
  17. return self.frame.size.width;
  18. }
  19. - (CGFloat)x
  20. {
  21. return self.frame.origin.x;
  22. }
  23. - (void)setX:(CGFloat)x
  24. {
  25. self.frame = CGRectMake(x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
  26. }
  27. - (void)setY:(CGFloat)y
  28. {
  29. self.frame = CGRectMake(self.frame.origin.x, y, self.frame.size.width, self.frame.size.height);
  30. }
  31. - (CGFloat)y
  32. {
  33. return self.frame.origin.y;
  34. }
  35. - (CGSize)size
  36. {
  37. return self.frame.size;
  38. }
  39. - (CGPoint)origin
  40. {
  41. return self.frame.origin;
  42. }
  43. - (CGFloat)centerX
  44. {
  45. return self.center.x;
  46. }
  47. - (CGFloat)centerY
  48. {
  49. return self.center.y;
  50. }
  51. - (CGFloat)bottom
  52. {
  53. return self.frame.size.height + self.frame.origin.y;
  54. }
  55. - (CGFloat)right
  56. {
  57. return self.frame.size.width + self.frame.origin.x;
  58. }
  59. - (void)setCornerRadius:(CGFloat)cornerRadius
  60. {
  61. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  62. CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
  63. //设置大小
  64. maskLayer.frame = self.bounds;
  65. //设置图形样子
  66. maskLayer.path = maskPath.CGPath;
  67. self.layer.mask = maskLayer;
  68. }
  69. // height
  70. - (void)setHeight:(CGFloat)height
  71. {
  72. CGRect newFrame = CGRectMake(self.x, self.y, self.width, height);
  73. self.frame = newFrame;
  74. }
  75. - (void)heightEqualToView:(UIView *)view
  76. {
  77. self.height = view.height;
  78. }
  79. // width
  80. - (void)setWidth:(CGFloat)width
  81. {
  82. CGRect newFrame = CGRectMake(self.x, self.y, width, self.height);
  83. self.frame = newFrame;
  84. }
  85. - (void)widthEqualToView:(UIView *)view
  86. {
  87. self.width = view.width;
  88. }
  89. // center
  90. - (void)setCenterX:(CGFloat)centerX
  91. {
  92. CGPoint center = CGPointMake(self.centerX, self.centerY);
  93. center.x = centerX;
  94. self.center = center;
  95. }
  96. - (void)setCenterY:(CGFloat)centerY
  97. {
  98. CGPoint center = CGPointMake(self.centerX, self.centerY);
  99. center.y = centerY;
  100. self.center = center;
  101. }
  102. - (void)centerXEqualToView:(UIView *)view
  103. {
  104. UIView *superView = view.superview ? view.superview : view;
  105. CGPoint viewCenterPoint = [superView convertPoint:view.center toView:self.topSuperView];
  106. CGPoint centerPoint = [self.topSuperView convertPoint:viewCenterPoint toView:self.superview];
  107. self.centerX = centerPoint.x;
  108. }
  109. - (void)centerYEqualToView:(UIView *)view
  110. {
  111. UIView *superView = view.superview ? view.superview : view;
  112. CGPoint viewCenterPoint = [superView convertPoint:view.center toView:self.topSuperView];
  113. CGPoint centerPoint = [self.topSuperView convertPoint:viewCenterPoint toView:self.superview];
  114. self.centerY = centerPoint.y;
  115. }
  116. // top, bottom, left, right
  117. - (void)top:(CGFloat)top FromView:(UIView *)view
  118. {
  119. UIView *superView = view.superview ? view.superview : view;
  120. CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
  121. CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
  122. self.y = newOrigin.y + top + view.height;
  123. }
  124. - (void)bottom:(CGFloat)bottom FromView:(UIView *)view
  125. {
  126. UIView *superView = view.superview ? view.superview : view;
  127. CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
  128. CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
  129. self.y = newOrigin.y - bottom - self.height;
  130. }
  131. - (void)left:(CGFloat)left FromView:(UIView *)view
  132. {
  133. UIView *superView = view.superview ? view.superview : view;
  134. CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
  135. CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
  136. self.x = newOrigin.x - left - self.width;
  137. }
  138. - (void)right:(CGFloat)right FromView:(UIView *)view
  139. {
  140. UIView *superView = view.superview ? view.superview : view;
  141. CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
  142. CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
  143. self.x = newOrigin.x + right + view.width;
  144. }
  145. - (void)topInContainer:(CGFloat)top shouldResize:(BOOL)shouldResize
  146. {
  147. if (shouldResize) {
  148. self.height = self.y - top + self.height;
  149. }
  150. self.y = top;
  151. }
  152. - (void)bottomInContainer:(CGFloat)bottom shouldResize:(BOOL)shouldResize
  153. {
  154. if (shouldResize) {
  155. self.height = self.superview.height - bottom - self.y;
  156. } else {
  157. self.y = self.superview.height - self.height - bottom;
  158. }
  159. }
  160. - (void)leftInContainer:(CGFloat)left shouldResize:(BOOL)shouldResize
  161. {
  162. if (shouldResize) {
  163. self.width = self.x - left + self.superview.width;
  164. }
  165. self.x = left;
  166. }
  167. - (void)rightInContainer:(CGFloat)right shouldResize:(BOOL)shouldResize
  168. {
  169. if (shouldResize) {
  170. self.width = self.superview.width - right - self.x;
  171. } else {
  172. self.x = self.superview.width - self.width - right;
  173. }
  174. }
  175. - (void)topEqualToView:(UIView *)view
  176. {
  177. UIView *superView = view.superview ? view.superview : view;
  178. CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
  179. CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
  180. self.y = newOrigin.y;
  181. }
  182. - (void)bottomEqualToView:(UIView *)view
  183. {
  184. UIView *superView = view.superview ? view.superview : view;
  185. CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
  186. CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
  187. self.y = newOrigin.y + view.height - self.height;
  188. }
  189. - (void)leftEqualToView:(UIView *)view
  190. {
  191. UIView *superView = view.superview ? view.superview : view;
  192. CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
  193. CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
  194. self.x = newOrigin.x;
  195. }
  196. - (void)rightEqualToView:(UIView *)view
  197. {
  198. UIView *superView = view.superview ? view.superview : view;
  199. CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
  200. CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
  201. self.x = newOrigin.x + view.width - self.width;
  202. }
  203. // size
  204. - (void)setSize:(CGSize)size
  205. {
  206. self.frame = CGRectMake(self.x, self.y, size.width, size.height);
  207. }
  208. - (void)sizeEqualToView:(UIView *)view
  209. {
  210. self.frame = CGRectMake(self.x, self.y, view.width, view.height);
  211. }
  212. // imbueset
  213. - (void)fillWidth
  214. {
  215. self.width = self.superview.width;
  216. }
  217. - (void)fillHeight
  218. {
  219. self.height = self.superview.height;
  220. }
  221. - (void)fill
  222. {
  223. self.frame = CGRectMake(0, 0, self.superview.width, self.superview.height);
  224. }
  225. - (UIView *)topSuperView
  226. {
  227. UIView *topSuperView = self.superview;
  228. if (topSuperView == nil) {
  229. topSuperView = self;
  230. } else {
  231. while (topSuperView.superview) {
  232. topSuperView = topSuperView.superview;
  233. }
  234. }
  235. return topSuperView;
  236. }
  237. - (void)removeAllSubviews {
  238. while (self.subviews.count) {
  239. UIView* child = self.subviews.lastObject;
  240. [child removeFromSuperview];
  241. }
  242. }
  243. @end