口袋优选

UIView+TYAutoLayout.m 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // UIView+TYAutoLayout.m
  3. // TYAlertControllerDemo
  4. //
  5. // Created by SunYong on 15/9/8.
  6. // Copyright (c) 2015年 tanyang. All rights reserved.
  7. //
  8. #import "UIView+TYAutoLayout.h"
  9. @implementation UIView (TYAutoLayout)
  10. - (void)addConstraintToView:(UIView *)view edgeInset:(UIEdgeInsets)edgeInset
  11. {
  12. [self addConstraintWithView:view topView:self leftView:self bottomView:self rightView:self edgeInset:edgeInset];
  13. }
  14. - (void)addConstraintWithView:(UIView *)view topView:(UIView *)topView leftView:(UIView *)leftView
  15. bottomView:(UIView *)bottomView rightView:(UIView *)rightView edgeInset:(UIEdgeInsets)edgeInset
  16. {
  17. if (topView) {
  18. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:topView attribute:NSLayoutAttributeTop multiplier:1 constant:edgeInset.top]];
  19. }
  20. if (leftView) {
  21. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:leftView attribute:NSLayoutAttributeLeft multiplier:1 constant:edgeInset.left]];
  22. }
  23. if (rightView) {
  24. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:rightView attribute:NSLayoutAttributeRight multiplier:1 constant:edgeInset.right]];
  25. }
  26. if (bottomView) {
  27. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:bottomView attribute:NSLayoutAttributeBottom multiplier:1 constant:edgeInset.bottom]];
  28. }
  29. }
  30. - (void)addConstraintWithLeftView:(UIView *)leftView toRightView:(UIView *)rightView constant:(CGFloat)constant
  31. {
  32. [self addConstraint:[NSLayoutConstraint constraintWithItem:leftView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:rightView attribute:NSLayoutAttributeLeft multiplier:1 constant:-constant]];
  33. }
  34. - (NSLayoutConstraint *)addConstraintWithTopView:(UIView *)topView toBottomView:(UIView *)bottomView constant:(CGFloat)constant
  35. {
  36. NSLayoutConstraint *topBottomConstraint =[NSLayoutConstraint constraintWithItem:topView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:bottomView attribute:NSLayoutAttributeTop multiplier:1 constant:-constant];
  37. [self addConstraint:topBottomConstraint];
  38. return topBottomConstraint;
  39. }
  40. - (void)addConstraintWidth:(CGFloat)width height:(CGFloat)height
  41. {
  42. if (width > 0) {
  43. [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:width]];
  44. }
  45. if (height > 0) {
  46. [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:height]];
  47. }
  48. }
  49. - (void)addConstraintEqualWithView:(UIView *)view widthToView:(UIView *)wView heightToView:(UIView *)hView
  50. {
  51. if (wView) {
  52. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:wView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
  53. }
  54. if (hView) {
  55. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:hView attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];
  56. }
  57. }
  58. - (void)addConstraintCenterXToView:(UIView *)xView centerYToView:(UIView *)yView
  59. {
  60. if (xView) {
  61. [self addConstraint:[NSLayoutConstraint constraintWithItem:xView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  62. }
  63. if (yView) {
  64. [self addConstraint:[NSLayoutConstraint constraintWithItem:yView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  65. }
  66. }
  67. - (NSLayoutConstraint *)addConstraintCenterYToView:(UIView *)yView constant:(CGFloat)constant;
  68. {
  69. if (yView) {
  70. NSLayoutConstraint *centerYConstraint = [NSLayoutConstraint constraintWithItem:yView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:constant];
  71. [self addConstraint:centerYConstraint];
  72. return centerYConstraint;
  73. }
  74. return nil;
  75. }
  76. - (void)removeConstraintWithAttribte:(NSLayoutAttribute)attr
  77. {
  78. for (NSLayoutConstraint *constraint in self.constraints) {
  79. if (constraint.firstAttribute == attr) {
  80. [self removeConstraint:constraint];
  81. break;
  82. }
  83. }
  84. }
  85. - (void)removeConstraintWithView:(UIView *)view attribute:(NSLayoutAttribute)attr
  86. {
  87. for (NSLayoutConstraint *constraint in self.constraints) {
  88. if (constraint.firstAttribute == attr && constraint.firstItem == view) {
  89. [self removeConstraint:constraint];
  90. break;
  91. }
  92. }
  93. }
  94. - (void)removeAllConstraints
  95. {
  96. [self removeConstraints:self.constraints];
  97. }
  98. @end