UIView+CustomAutoLayout.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // UIView+CustomAutoLayout.h
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 13-11-20.
  6. // Copyright (c) 2013年 ywchen. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. // 使用装饰模式进行自动布局处理
  10. #ifdef DEBUG
  11. #define IsArgInvalid(curView, brotherView) NSAssert((self.superview == brotherView.superview) && !(CGRectEqualToRect(brotherView.frame, CGRectZero)), @"UIView (CustomAutoLayout)参数出错")
  12. #define IsSuperViewInvalid(curView) NSAssert((self.superview != nil), @"UIView (CustomAutoLayout)父控件没有设置参数出错")
  13. #else
  14. #define IsArgInvalid(curView, brotherView)
  15. #define IsSuperViewInvalid(curView)
  16. #endif
  17. @interface UIView (CustomAutoLayout)
  18. - (UIView *)sameWith:(UIView *)brotherView;
  19. // 同一控件内两子之空间之间的相对位置
  20. // margin 之间的空隙
  21. // brotherView.superView与self.superView相同
  22. // brotherView必须要先设置了frame
  23. // 会影响origin的位置,会影响其高度
  24. - (UIView *)layoutAbove:(UIView *)brotherView;
  25. - (UIView *)layoutAbove:(UIView *)brotherView margin:(CGFloat)margin;
  26. - (UIView *)layoutBelow:(UIView *)brotherView;
  27. - (UIView *)layoutBelow:(UIView *)brotherView margin:(CGFloat)margin;
  28. // 会影响origin的位置,会影响其宽度
  29. - (UIView *)layoutToLeftOf:(UIView *)brotherView;
  30. - (UIView *)layoutToLeftOf:(UIView *)brotherView margin:(CGFloat)margin;
  31. - (UIView *)layoutToRightOf:(UIView *)brotherView;
  32. - (UIView *)layoutToRightOf:(UIView *)brotherView margin:(CGFloat)margin;
  33. // 填充式布局
  34. - (UIView *)scaleToAboveOf:(UIView *)brotherView;
  35. - (UIView *)scaleToAboveOf:(UIView *)brotherView margin:(CGFloat)margin;
  36. - (UIView *)scaleToBelowOf:(UIView *)brotherView;
  37. - (UIView *)scaleToBelowOf:(UIView *)brotherView margin:(CGFloat)margin;
  38. - (UIView *)scaleToLeftOf:(UIView *)brotherView;
  39. - (UIView *)scaleToLeftOf:(UIView *)brotherView margin:(CGFloat)margin;
  40. - (UIView *)scaleToRightOf:(UIView *)brotherView;
  41. - (UIView *)scaleToRightOf:(UIView *)brotherView margin:(CGFloat)margin;
  42. // 相对父控件进行填充
  43. - (UIView *)scaleParent;
  44. - (UIView *)scaleToParentTop;
  45. - (UIView *)scaleToParentTopWithMargin:(CGFloat)margin;
  46. - (UIView *)scaleToParentBottom;
  47. - (UIView *)scaleToParentBottomWithMargin:(CGFloat)margin;
  48. - (UIView *)scaleToParentLeft;
  49. - (UIView *)scaleToParentLeftWithMargin:(CGFloat)margin;
  50. - (UIView *)scaleToParentRight;
  51. - (UIView *)scaleToParentRightWithMargin:(CGFloat)margin;
  52. // 同一控件内两子之空间之间的对齐关系
  53. // 会影响origin的位置, 不影响大小
  54. - (UIView *)alignTop:(UIView *)brotherView;
  55. - (UIView *)alignTop:(UIView *)brotherView margin:(CGFloat)margin;
  56. - (UIView *)alignBottom:(UIView *)brotherView;
  57. - (UIView *)alignBottom:(UIView *)brotherView margin:(CGFloat)margin;
  58. - (UIView *)alignLeft:(UIView *)brotherView;
  59. - (UIView *)alignLeft:(UIView *)brotherView margin:(CGFloat)margin;
  60. - (UIView *)alignRight:(UIView *)brotherView;
  61. - (UIView *)alignRight:(UIView *)brotherView margin:(CGFloat)margin;
  62. - (UIView *)alignHorizontalCenterOf:(UIView *)brotherView;
  63. - (UIView *)alignVerticalCenterOf:(UIView *)brotherView;
  64. - (UIView *)alignCenterOf:(UIView *)brotherView;
  65. - (UIView *)moveCenterTo:(CGPoint)center;
  66. - (UIView *)move:(CGPoint)vec;
  67. // 与父控件对齐的关系
  68. // 只影响其坐标位置,不影响其大小
  69. - (UIView *)alignParentTop;
  70. - (UIView *)alignParentBottom;
  71. - (UIView *)alignParentLeft;
  72. - (UIView *)alignParentRight;
  73. - (UIView *)alignParentCenter;
  74. - (UIView *)alignParentCenter:(CGPoint)margin;
  75. - (UIView *)alignParentTopWithMargin:(CGFloat)margin;
  76. - (UIView *)alignParentBottomWithMargin:(CGFloat)margin;
  77. - (UIView *)alignParentLeftWithMargin:(CGFloat)margin;
  78. - (UIView *)alignParentRightWithMargin:(CGFloat)margin;
  79. // 与父控件的边距
  80. // 只影响其坐标位置,影响其大小
  81. - (UIView *)marginParetnTop:(CGFloat)top bottom:(CGFloat)bottom left:(CGFloat)left rigth:(CGFloat)right;
  82. - (UIView *)marginParentWithEdgeInsets:(UIEdgeInsets)inset;
  83. - (UIView *)marginParetn:(CGFloat)margin;
  84. - (UIView *)marginParetnHorizontal:(CGFloat)margin;
  85. - (UIView *)marginParetnVertical:(CGFloat)margin;
  86. - (UIView *)marginParentTop:(CGFloat)margin;
  87. - (UIView *)marginParentBottom:(CGFloat)margin;
  88. - (UIView *)marginParentLeft:(CGFloat)margin;
  89. - (UIView *)marginParentRight:(CGFloat)margin;
  90. // 控件在父控控件中的位置
  91. // 水平居中
  92. // 只影响其坐标位置,不影响其大小
  93. - (UIView *)layoutParentHorizontalCenter;
  94. // 垂直居中
  95. - (UIView *)layoutParentVerticalCenter;
  96. // 居中
  97. - (UIView *)layoutParentCenter;
  98. // 与其他控件的大小关系
  99. // 影响其大小
  100. - (UIView *)widthEqualTo:(UIView *)brotherView;
  101. - (UIView *)heigthEqualTo:(UIView *)brotherView;
  102. - (UIView *)sizeEqualTo:(UIView *)brotherView;
  103. - (UIView *)sizeWith:(CGSize)size;
  104. - (UIView *)setWidth:(CGFloat)width;
  105. - (UIView *)setHeight:(CGFloat)height;
  106. - (UIView *)shrink:(CGSize)size;
  107. - (UIView *)shrinkHorizontal:(CGFloat)margin;
  108. - (UIView *)shrinkVertical:(CGFloat)margin;
  109. - (UIView *)alignViews:(NSArray *)array isSubView:(BOOL)isSub padding:(CGFloat)padding margin:(CGFloat)margin horizontal:(BOOL)ishorizontal inRect:(CGRect)rect;
  110. // views里面的View都是按UI的指定顺序放好的
  111. - (UIView *)alignSubviews:(NSArray *)views horizontallyWithPadding:(CGFloat)padding margin:(CGFloat)margin inRect:(CGRect)rect;
  112. - (UIView *)alignSubviews:(NSArray *)views verticallyWithPadding:(CGFloat)padding margin:(CGFloat)margin inRect:(CGRect)rect;
  113. - (UIView *)alignSubviewsHorizontallyWithPadding:(CGFloat)padding margin:(CGFloat)margin;
  114. - (UIView *)alignSubviewsVerticallyWithPadding:(CGFloat)padding margin:(CGFloat)margin;
  115. // 使views以Grid方式均匀显示
  116. - (UIView *)gridViews:(NSArray *)views inColumn:(NSInteger)column size:(CGSize)cellSize margin:(CGSize)margin inRect:(CGRect)rect;
  117. @end