UIView+CustomAutoLayout.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. //
  2. // UIView+CustomAutoLayout.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 13-11-20.
  6. // Copyright (c) 2013年 ywchen. All rights reserved.
  7. //
  8. #import "UIView+CustomAutoLayout.h"
  9. @implementation UIView (CustomAutoLayout)
  10. - (UIView *)sameWith:(UIView *)brotherView
  11. {
  12. IsArgInvalid(self, brotherView);
  13. self.frame = brotherView.frame;
  14. return self;
  15. }
  16. // 同一控件内两子之空间之间的相对位置
  17. // margin 之间的空隙
  18. // brotherView.superView与self.superView相同
  19. // brotherView必须要先设置了frame
  20. //- (BOOL)isArgValid:(UIView *)brotherView
  21. //{
  22. // return (self.superview == brotherView.superview) && (CGRectEqualToRect(brotherView.frame, CGRectZero));
  23. //}
  24. // 会影响origin的位置,会影响其高度
  25. - (UIView *)layoutAbove:(UIView *)brotherView
  26. {
  27. return [self layoutAbove:brotherView margin:0];
  28. }
  29. - (UIView *)layoutAbove:(UIView *)brotherView margin:(CGFloat)margin
  30. {
  31. // BOOL vailed = (self.superview == brotherView.superview) && !(CGRectEqualToRect(brotherView.frame, CGRectZero));
  32. // NSAssert(vailed, @"UIView (CustomAutoLayout)参数出错");
  33. IsArgInvalid(self, brotherView);
  34. CGRect rect = self.frame;
  35. CGRect brect = brotherView.frame;
  36. rect.origin.y = brect.origin.y - margin - rect.size.height;
  37. self.frame = rect;
  38. return self;
  39. }
  40. - (UIView *)layoutBelow:(UIView *)brotherView
  41. {
  42. return [self layoutBelow:brotherView margin:0];
  43. }
  44. - (UIView *)layoutBelow:(UIView *)brotherView margin:(CGFloat)margin
  45. {
  46. IsArgInvalid(self, brotherView);
  47. CGRect rect = self.frame;
  48. CGRect brect = brotherView.frame;
  49. rect.origin.y = brect.origin.y + brect.size.height + margin;
  50. self.frame = rect;
  51. return self;
  52. }
  53. // 会影响origin的位置,会影响其宽度
  54. - (UIView *)layoutToLeftOf:(UIView *)brotherView
  55. {
  56. return [self layoutToLeftOf:brotherView margin:0];
  57. }
  58. - (UIView *)layoutToLeftOf:(UIView *)brotherView margin:(CGFloat)margin
  59. {
  60. IsArgInvalid(self, brotherView);
  61. CGRect rect = self.frame;
  62. CGRect brect = brotherView.frame;
  63. rect.origin.x = brect.origin.x - margin - rect.size.width;
  64. self.frame = rect;
  65. return self;
  66. }
  67. - (UIView *)layoutToRightOf:(UIView *)brotherView
  68. {
  69. return [self layoutToRightOf:brotherView margin:0];
  70. }
  71. - (UIView *)layoutToRightOf:(UIView *)brotherView margin:(CGFloat)margin
  72. {
  73. IsArgInvalid(self, brotherView);
  74. CGRect rect = self.frame;
  75. CGRect brect = brotherView.frame;
  76. rect.origin.x = brect.origin.x + brect.size.width + margin;
  77. self.frame = rect;
  78. return self;
  79. }
  80. ///填充式布局
  81. - (UIView *)scaleToAboveOf:(UIView *)brotherView
  82. {
  83. return [self scaleToAboveOf:brotherView margin:0];
  84. }
  85. - (UIView *)scaleToAboveOf:(UIView *)brotherView margin:(CGFloat)margin
  86. {
  87. IsArgInvalid(self, brotherView);
  88. CGRect rect = self.frame;
  89. CGRect brect = brotherView.frame;
  90. if (brect.origin.y > rect.origin.y)
  91. {
  92. rect.size.height = brect.origin.y - margin - rect.origin.y;
  93. }
  94. else
  95. {
  96. rect.size.height = rect.origin.y + rect.size.height - (brect.origin.y + margin);
  97. rect.origin.y = brect.origin.y + margin;
  98. }
  99. self.frame = rect;
  100. return self;
  101. }
  102. - (UIView *)scaleToBelowOf:(UIView *)brotherView
  103. {
  104. return [self scaleToBelowOf:brotherView margin:0];
  105. }
  106. - (UIView *)scaleToBelowOf:(UIView *)brotherView margin:(CGFloat)margin
  107. {
  108. IsArgInvalid(self, brotherView);
  109. CGRect rect = self.frame;
  110. CGRect brect = brotherView.frame;
  111. if (rect.origin.y < brect.origin.y + brect.size.height) {
  112. rect.size.height = brect.origin.y + brect.size.height - margin - rect.origin.y;
  113. }
  114. else
  115. {
  116. rect.size.height = rect.origin.y + rect.size.height - (margin + brect.origin.y + brect.size.height);
  117. rect.origin.y = (margin + brect.origin.y + brect.size.height);
  118. }
  119. self.frame = rect;
  120. return self;
  121. }
  122. - (UIView *)scaleToLeftOf:(UIView *)brotherView
  123. {
  124. return [self scaleToLeftOf:brotherView margin:0];
  125. }
  126. - (UIView *)scaleToLeftOf:(UIView *)brotherView margin:(CGFloat)margin
  127. {
  128. IsArgInvalid(self, brotherView);
  129. CGRect rect = self.frame;
  130. CGRect brect = brotherView.frame;
  131. if (rect.origin.x < brect.origin.x)
  132. {
  133. rect.size.width = brect.origin.x - margin - rect.origin.x;
  134. }
  135. else
  136. {
  137. rect.size.width = rect.origin.x + rect.size.width - (margin + brect.origin.x);
  138. rect.origin.x = brect.origin.x + margin;
  139. }
  140. self.frame = rect;
  141. return self;
  142. }
  143. - (UIView *)scaleToRightOf:(UIView *)brotherView
  144. {
  145. return [self scaleToRightOf:brotherView margin:0];
  146. }
  147. - (UIView *)scaleToRightOf:(UIView *)brotherView margin:(CGFloat)margin
  148. {
  149. IsArgInvalid(self, brotherView);
  150. CGRect rect = self.frame;
  151. CGRect brect = brotherView.frame;
  152. if (rect.origin.x < brect.origin.x + brect.size.width)
  153. {
  154. rect.size.width = brect.origin.x + brect.size.width - margin - rect.origin.x;
  155. }
  156. else
  157. {
  158. rect.size.width = rect.origin.x + rect.size.width - (brect.origin.x + brect.size.width - margin);
  159. rect.origin.x = brect.origin.x + brect.size.width - margin;
  160. }
  161. self.frame = rect;
  162. return self;
  163. }
  164. - (UIView *)scaleParent
  165. {
  166. self.frame = self.superview.bounds;
  167. return self;
  168. }
  169. - (UIView *)scaleToParentTop
  170. {
  171. return [self scaleToParentTopWithMargin:0];
  172. }
  173. - (UIView *)scaleToParentTopWithMargin:(CGFloat)margin
  174. {
  175. IsSuperViewInvalid(self);
  176. CGRect sRect = self.superview.bounds;
  177. CGRect rect = self.frame;
  178. rect.size.height = rect.origin.y + rect.size.height - (sRect.origin.y + margin);
  179. rect.origin.y = sRect.origin.y + margin;
  180. self.frame = rect;
  181. return self;
  182. }
  183. - (UIView *)scaleToParentBottom
  184. {
  185. return [self scaleToParentBottomWithMargin:0];
  186. }
  187. - (UIView *)scaleToParentBottomWithMargin:(CGFloat)margin
  188. {
  189. IsSuperViewInvalid(self);
  190. CGRect sRect = self.superview.bounds;
  191. CGRect rect = self.frame;
  192. rect.size.height = sRect.origin.y + sRect.size.height - margin - rect.origin.y;
  193. self.frame = rect;
  194. return self;
  195. }
  196. - (UIView *)scaleToParentLeft
  197. {
  198. return [self scaleToParentLeftWithMargin:0];
  199. }
  200. - (UIView *)scaleToParentLeftWithMargin:(CGFloat)margin
  201. {
  202. IsSuperViewInvalid(self);
  203. CGRect sRect = self.superview.bounds;
  204. CGRect rect = self.frame;
  205. rect.size.width = rect.origin.x + rect.size.width - (sRect.origin.x + margin);
  206. rect.origin.x = sRect.origin.x + margin;
  207. self.frame = rect;
  208. return self;
  209. }
  210. - (UIView *)scaleToParentRight
  211. {
  212. return [self scaleToParentRightWithMargin:0];
  213. }
  214. - (UIView *)scaleToParentRightWithMargin:(CGFloat)margin
  215. {
  216. IsSuperViewInvalid(self);
  217. CGRect sRect = self.superview.bounds;
  218. CGRect rect = self.frame;
  219. rect.size.width = sRect.origin.x + sRect.size.width - margin - rect.origin.x;
  220. self.frame = rect;
  221. return self;
  222. }
  223. // 同一控件内两子之空间之间的对齐关系
  224. // 会影响origin的位置, 不影响大小
  225. - (UIView *)alignTop:(UIView *)brotherView
  226. {
  227. return [self alignTop:brotherView margin:0];
  228. }
  229. - (UIView *)alignTop:(UIView *)brotherView margin:(CGFloat)margin
  230. {
  231. IsArgInvalid(self, brotherView);
  232. CGRect rect = self.frame;
  233. CGRect brect = brotherView.frame;
  234. rect.origin.y = brect.origin.y - margin;
  235. self.frame = rect;
  236. return self;
  237. }
  238. - (UIView *)alignBottom:(UIView *)brotherView
  239. {
  240. return [self alignBottom:brotherView margin:0];
  241. }
  242. - (UIView *)alignBottom:(UIView *)brotherView margin:(CGFloat)margin
  243. {
  244. IsArgInvalid(self, brotherView);
  245. CGRect rect = self.frame;
  246. CGRect brect = brotherView.frame;
  247. rect.origin.y = brect.origin.y + brect.size.height - rect.size.height - margin;
  248. self.frame = rect;
  249. return self;
  250. }
  251. - (UIView *)alignLeft:(UIView *)brotherView
  252. {
  253. return [self alignLeft:brotherView margin:0];
  254. }
  255. - (UIView *)alignLeft:(UIView *)brotherView margin:(CGFloat)margin
  256. {
  257. IsArgInvalid(self, brotherView);
  258. CGRect rect = self.frame;
  259. rect.origin.x = brotherView.frame.origin.x + margin;
  260. self.frame = rect;
  261. return self;
  262. }
  263. - (UIView *)alignRight:(UIView *)brotherView
  264. {
  265. return [self alignRight:brotherView margin:0];
  266. }
  267. - (UIView *)alignRight:(UIView *)brotherView margin:(CGFloat)margin
  268. {
  269. IsArgInvalid(self, brotherView);
  270. CGRect rect = self.frame;
  271. CGRect brect = brotherView.frame;
  272. rect.origin.x = brect.origin.x + brect.size.width - rect.size.width - margin;
  273. self.frame = rect;
  274. return self;
  275. }
  276. - (UIView *)alignHorizontalCenterOf:(UIView *)brotherView
  277. {
  278. IsArgInvalid(self, brotherView);
  279. CGPoint bCenter = brotherView.center;
  280. CGPoint center = self.center;
  281. center.x = bCenter.x;
  282. self.center = center;
  283. return self;
  284. }
  285. - (UIView *)alignVerticalCenterOf:(UIView *)brotherView
  286. {
  287. IsArgInvalid(self, brotherView);
  288. CGPoint bCenter = brotherView.center;
  289. CGPoint center = self.center;
  290. center.y = bCenter.y;
  291. self.center = center;
  292. return self;
  293. }
  294. - (UIView *)alignCenterOf:(UIView *)brotherView
  295. {
  296. IsArgInvalid(self, brotherView);
  297. self.center = brotherView.center;
  298. return self;
  299. }
  300. - (UIView *)moveCenterTo:(CGPoint)center
  301. {
  302. self.center = center;
  303. return self;
  304. }
  305. - (UIView *)move:(CGPoint)vec
  306. {
  307. CGPoint c = self.center;
  308. c.x += vec.x;
  309. c.y += vec.y;
  310. self.center = c;
  311. return self;
  312. }
  313. // 与父控件对齐的关系
  314. // 只影响其坐标位置,不影响其大小
  315. - (UIView *)alignParentTop
  316. {
  317. return [self alignParentLeftWithMargin:0];
  318. }
  319. - (UIView *)alignParentBottom
  320. {
  321. return [self alignParentBottomWithMargin:0];
  322. }
  323. - (UIView *)alignParentLeft
  324. {
  325. return [self alignParentLeftWithMargin:0];
  326. }
  327. - (UIView *)alignParentRight
  328. {
  329. return [self alignParentRightWithMargin:0];
  330. }
  331. - (UIView *)alignParentCenter
  332. {
  333. return [self alignParentCenter:CGPointMake(0, 0)];
  334. }
  335. - (UIView *)alignParentCenter:(CGPoint)margin
  336. {
  337. IsSuperViewInvalid(self);
  338. CGRect sbounds = self.superview.bounds;
  339. CGPoint center = CGPointMake(CGRectGetMidX(sbounds), CGRectGetMidY(sbounds));
  340. center.x += margin.x;
  341. center.y += margin.y;
  342. self.center = center;
  343. return self;
  344. }
  345. - (UIView *)alignParentTopWithMargin:(CGFloat)margin
  346. {
  347. IsSuperViewInvalid(self);
  348. CGRect rect = self.frame;
  349. rect.origin.y = self.superview.bounds.origin.y + margin;
  350. self.frame = rect;
  351. return self;
  352. }
  353. - (UIView *)alignParentBottomWithMargin:(CGFloat)margin
  354. {
  355. IsSuperViewInvalid(self);
  356. CGRect superBounds = self.superview.bounds;
  357. CGRect rect = self.frame;
  358. rect.origin.y = superBounds.origin.y + superBounds.size.height - margin - rect.size.height;
  359. self.frame = rect;
  360. return self;
  361. }
  362. - (UIView *)alignParentLeftWithMargin:(CGFloat)margin
  363. {
  364. IsSuperViewInvalid(self);
  365. CGRect rect = self.frame;
  366. rect.origin.x = self.superview.bounds.origin.x + margin;;
  367. self.frame = rect;
  368. return self;
  369. }
  370. - (UIView *)alignParentRightWithMargin:(CGFloat)margin
  371. {
  372. IsSuperViewInvalid(self);
  373. CGRect superBounds = self.superview.bounds;
  374. CGRect rect = self.frame;
  375. rect.origin.x = superBounds.origin.x + superBounds.size.width - margin - rect.size.width;
  376. self.frame = rect;
  377. return self;
  378. }
  379. // 与父控件的边距
  380. // 只影响其坐标位置,不影响其大小
  381. - (UIView *)marginParetnTop:(CGFloat)top bottom:(CGFloat)bottom left:(CGFloat)left rigth:(CGFloat)right
  382. {
  383. IsSuperViewInvalid(self);
  384. CGRect superBounds = self.superview.bounds;
  385. CGRect rect = CGRectZero;
  386. rect.origin.y = top;
  387. rect.size.height = superBounds.origin.y + superBounds.size.height - rect.origin.y - bottom;
  388. rect.origin.x = left;
  389. rect.size.width = superBounds.origin.x + superBounds.size.width - rect.origin.x - right;
  390. self.frame = rect;
  391. return self;
  392. }
  393. - (UIView *)marginParentWithEdgeInsets:(UIEdgeInsets)inset
  394. {
  395. return [self marginParetnTop:inset.top bottom:inset.bottom left:inset.left rigth:inset.right];
  396. }
  397. - (UIView *)marginParetn:(CGFloat)margin
  398. {
  399. IsSuperViewInvalid(self);
  400. self.frame = CGRectInset(self.superview.bounds, margin, margin);
  401. return self;
  402. }
  403. - (UIView *)marginParetnHorizontal:(CGFloat)margin
  404. {
  405. IsSuperViewInvalid(self);
  406. CGRect supRect = self.superview.bounds;
  407. CGRect rect = self.frame;
  408. rect.origin.x = supRect.origin.x + margin;
  409. rect.size.width = supRect.size.width - 2 * margin;
  410. self.frame = rect;
  411. return self;
  412. }
  413. - (UIView *)marginParetnVertical:(CGFloat)margin
  414. {
  415. IsSuperViewInvalid(self);
  416. CGRect supRect = self.superview.bounds;
  417. CGRect rect = self.frame;
  418. rect.origin.y = supRect.origin.y + margin;
  419. rect.size.height = supRect.size.height - 2 * margin;
  420. self.frame = rect;
  421. return self;
  422. }
  423. - (UIView *)marginParentTop:(CGFloat)margin
  424. {
  425. IsSuperViewInvalid(self);
  426. CGRect supRect = self.superview.bounds;
  427. CGRect rect = self.frame;
  428. rect.origin.y = supRect.origin.y + margin;
  429. self.frame = rect;
  430. return self;
  431. }
  432. - (UIView *)marginParentBottom:(CGFloat)margin
  433. {
  434. IsSuperViewInvalid(self);
  435. CGRect supRect = self.superview.bounds;
  436. CGRect rect = self.frame;
  437. rect.size.height = supRect.origin.y + supRect.size.height - margin - rect.origin.y;
  438. self.frame = rect;
  439. return self;
  440. }
  441. - (UIView *)marginParentLeft:(CGFloat)margin
  442. {
  443. IsSuperViewInvalid(self);
  444. CGRect supRect = self.superview.bounds;
  445. CGRect rect = self.frame;
  446. rect.origin.x = supRect.origin.x + margin;
  447. self.frame = rect;
  448. return self;
  449. }
  450. - (UIView *)marginParentRight:(CGFloat)margin
  451. {
  452. IsSuperViewInvalid(self);
  453. CGRect supRect = self.superview.bounds;
  454. CGRect rect = self.frame;
  455. rect.size.width = supRect.origin.x + supRect.size.width - margin - rect.origin.x;
  456. self.frame = rect;
  457. return self;
  458. }
  459. // 控件在父控控件中的位置
  460. // 水平居中
  461. // 只影响其坐标位置,不影响其大小
  462. - (UIView *)layoutParentHorizontalCenter
  463. {
  464. IsSuperViewInvalid(self);
  465. CGPoint center = CGPointZero;
  466. CGRect srect = self.superview.bounds;
  467. center.x = CGRectGetMidX(srect);;
  468. center.y = self.center.y;
  469. self.center = center;
  470. return self;
  471. }
  472. // 垂直居中
  473. - (UIView *)layoutParentVerticalCenter
  474. {
  475. IsSuperViewInvalid(self);
  476. CGPoint center = CGPointZero;
  477. center.x = self.center.x;
  478. CGRect srect = self.superview.bounds;
  479. center.y = CGRectGetMidY(srect);
  480. self.center = center;
  481. return self;
  482. }
  483. // 居中
  484. - (UIView *)layoutParentCenter
  485. {
  486. IsSuperViewInvalid(self);
  487. CGRect rect = self.superview.bounds;
  488. self.center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
  489. return self;
  490. }
  491. // 与其他控件的大小关系
  492. // 影响其大小
  493. - (UIView *)widthEqualTo:(UIView *)brotherView
  494. {
  495. IsArgInvalid(self, brotherView);
  496. CGRect brect = brotherView.frame;
  497. CGRect srect = self.frame;
  498. srect.size.width = brect.size.width;
  499. self.frame = srect;
  500. return self;
  501. }
  502. - (UIView *)heigthEqualTo:(UIView *)brotherView
  503. {
  504. IsArgInvalid(self, brotherView);
  505. CGRect brect = brotherView.frame;
  506. CGRect srect = self.frame;
  507. srect.size.height = brect.size.height;
  508. self.frame = srect;
  509. return self;
  510. }
  511. - (UIView *)sizeEqualTo:(UIView *)brotherView
  512. {
  513. IsArgInvalid(self, brotherView);
  514. CGRect brect = brotherView.frame;
  515. CGRect srect = self.frame;
  516. srect.size = brect.size;
  517. self.frame = srect;
  518. return self;
  519. }
  520. - (UIView *)sizeWith:(CGSize)size
  521. {
  522. CGRect rect = CGRectZero;
  523. rect.size = size;
  524. self.frame = rect;
  525. return self;
  526. }
  527. - (UIView *)setWidth:(CGFloat)width
  528. {
  529. CGRect rect = self.frame;
  530. rect.size.width = width;
  531. self.frame = rect;
  532. return self;
  533. }
  534. - (UIView *)setHeight:(CGFloat)height
  535. {
  536. CGRect rect = self.frame;
  537. rect.size.height = height;
  538. self.frame = rect;
  539. return self;
  540. }
  541. - (UIView *)shrink:(CGSize)size
  542. {
  543. CGRect rect = self.frame;
  544. self.frame = CGRectInset(rect, size.width, size.height);
  545. return self;
  546. }
  547. - (UIView *)shrinkHorizontal:(CGFloat)margin
  548. {
  549. CGRect rect = self.frame;
  550. self.frame = CGRectInset(rect, margin, 0);
  551. return self;
  552. }
  553. - (UIView *)shrinkVertical:(CGFloat)margin
  554. {
  555. CGRect rect = self.frame;
  556. self.frame = CGRectInset(rect, 0, margin);
  557. return self;
  558. }
  559. // views里面的View都是按UI的指定顺序放好的
  560. - (UIView *)alignViews:(NSArray *)array isSubView:(BOOL)isSub padding:(CGFloat)padding margin:(CGFloat)margin horizontal:(BOOL)ishorizontal inRect:(CGRect)rect
  561. {
  562. BOOL isSameParent = YES;
  563. if (!isSub)
  564. {
  565. for (UIView *view in array)
  566. {
  567. isSameParent = isSameParent && (view.superview == self);
  568. if (!isSameParent)
  569. {
  570. // DebugLog(@"所排列的View的父控件不同");
  571. return self;
  572. break;
  573. }
  574. }
  575. }
  576. if (ishorizontal)
  577. {
  578. CGRect marginRect = CGRectInset(rect, margin, 0);
  579. NSInteger count = array.count;
  580. const CGFloat kWidth = (marginRect.size.width - (count - 1)*padding)/count;
  581. CGFloat startX = marginRect.origin.x;
  582. for (UIView *view in array)
  583. {
  584. CGRect rect = view.frame;
  585. rect.origin.y = marginRect.origin.y;
  586. rect.size.height = marginRect.size.height;
  587. rect.origin.x = startX;
  588. rect.size.width = kWidth;
  589. view.frame = rect;
  590. startX += padding + kWidth;
  591. }
  592. }
  593. else
  594. {
  595. CGRect marginRect = CGRectInset(rect, 0, margin);
  596. NSInteger count = array.count;
  597. const CGFloat kHeight = (marginRect.size.width - (count - 1)*padding)/count;
  598. CGFloat startY = marginRect.origin.y;
  599. for (UIView *view in array)
  600. {
  601. CGRect viewrect = view.frame;
  602. rect.origin.x = marginRect.origin.x;
  603. rect.size.width = marginRect.size.width;
  604. viewrect.origin.y = startY;
  605. viewrect.size.height = kHeight;
  606. view.frame = viewrect;
  607. startY += padding + kHeight;
  608. }
  609. }
  610. return self;
  611. }
  612. - (UIView *)alignSubviewsHorizontallyWithPadding:(CGFloat)padding margin:(CGFloat)margin
  613. {
  614. return [self alignViews:self.subviews isSubView:YES padding:padding margin:margin horizontal:YES inRect:self.bounds];
  615. }
  616. - (UIView *)alignSubviewsVerticallyWithPadding:(CGFloat)padding margin:(CGFloat)margin;
  617. {
  618. return[self alignViews:self.subviews isSubView:YES padding:padding margin:margin horizontal:NO inRect:self.bounds];
  619. }
  620. - (UIView *)alignSubviews:(NSArray *)views horizontallyWithPadding:(CGFloat)padding margin:(CGFloat)margin inRect:(CGRect)rect
  621. {
  622. return [self alignViews:views isSubView:NO padding:padding margin:margin horizontal:YES inRect:rect];
  623. }
  624. - (UIView *)alignSubviews:(NSArray *)views verticallyWithPadding:(CGFloat)padding margin:(CGFloat)margin inRect:(CGRect)rect
  625. {
  626. return [self alignViews:views isSubView:NO padding:padding margin:margin horizontal:NO inRect:rect];
  627. }
  628. - (UIView *)gridViews:(NSArray *)views inColumn:(NSInteger)column size:(CGSize)cellSize margin:(CGSize)margin inRect:(CGRect)rect
  629. {
  630. CGSize menuSize = cellSize;
  631. const NSInteger kColumn = column;
  632. const NSInteger kCount = views.count;
  633. const NSInteger kRow = kCount % kColumn == 0 ? kCount / kColumn : kCount / kColumn + 1;
  634. const CGSize kSpaceSize = margin;
  635. CGRect menuCellRect = rect;
  636. menuCellRect.size = menuSize;
  637. for (NSInteger i = 0; i < kRow; i++)
  638. {
  639. for (NSInteger j = 0; j < kColumn; j++)
  640. {
  641. NSInteger index = i * kColumn + j;
  642. if (index >= kCount)
  643. {
  644. return self;
  645. }
  646. UIView *view = [views objectAtIndex:index];
  647. view.frame = menuCellRect;
  648. menuCellRect.origin.x += menuCellRect.size.width + kSpaceSize.width;
  649. }
  650. menuCellRect.origin.x = rect.origin.x;
  651. menuCellRect.origin.y += menuCellRect.size.height + kSpaceSize.height;
  652. }
  653. return self;
  654. }
  655. @end