口袋优选

UIView+ScottAutoLayout.m 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // UIView+ScottAutoLayout.m
  3. // QQLive
  4. //
  5. // Created by Scott_Mr on 2016/12/1.
  6. // Copyright © 2016年 Scott. All rights reserved.
  7. //
  8. #import "UIView+ScottAutoLayout.h"
  9. @implementation UIView (ScottAutoLayout)
  10. - (void)scott_addConstraintToView:(UIView *)view edgeInset:(UIEdgeInsets)edgeInset {
  11. if (view.translatesAutoresizingMaskIntoConstraints) {
  12. view.translatesAutoresizingMaskIntoConstraints = NO;
  13. }
  14. [self scott_addConstraintWithView:view topView:self leftView:self bottomView:self rightView:self edgeInset:edgeInset];
  15. }
  16. - (void)scott_addConstraintWithView:(UIView *)view topView:(UIView *)topView leftView:(UIView *)leftView bottomView:(UIView *)bottomView rightView:(UIView *)rightView edgeInset:(UIEdgeInsets)edgeInset
  17. {
  18. if (view.translatesAutoresizingMaskIntoConstraints) {
  19. view.translatesAutoresizingMaskIntoConstraints = NO;
  20. }
  21. if (topView) {
  22. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:topView attribute:NSLayoutAttributeTop multiplier:1 constant:edgeInset.top]];
  23. }
  24. if (leftView) {
  25. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:leftView attribute:NSLayoutAttributeLeft multiplier:1 constant:edgeInset.left]];
  26. }
  27. if (rightView) {
  28. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:rightView attribute:NSLayoutAttributeRight multiplier:1 constant:edgeInset.right]];
  29. }
  30. if (bottomView) {
  31. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:bottomView attribute:NSLayoutAttributeBottom multiplier:1 constant:edgeInset.bottom]];
  32. }
  33. }
  34. - (NSLayoutConstraint *)scott_addConstraintWithLeftView:(UIView *)leftView toRightView:(UIView *)rightView constant:(CGFloat)constant {
  35. if (leftView.translatesAutoresizingMaskIntoConstraints) {
  36. leftView.translatesAutoresizingMaskIntoConstraints = NO;
  37. }
  38. NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:leftView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:rightView attribute:NSLayoutAttributeLeft multiplier:1 constant:-constant];
  39. [self addConstraint:rightConstraint];
  40. return rightConstraint;
  41. }
  42. - (NSLayoutConstraint *)scott_addConstraintWithTopView:(UIView *)topView toBottomView:(UIView *)bottomView constant:(CGFloat)constant
  43. {
  44. if (topView.translatesAutoresizingMaskIntoConstraints) {
  45. topView.translatesAutoresizingMaskIntoConstraints = NO;
  46. }
  47. NSLayoutConstraint *topBottomConstraint =[NSLayoutConstraint constraintWithItem:topView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:bottomView attribute:NSLayoutAttributeTop multiplier:1 constant:-constant];
  48. [self addConstraint:topBottomConstraint];
  49. return topBottomConstraint;
  50. }
  51. - (void)scott_addConstraintWidth:(CGFloat)width height:(CGFloat)height {
  52. if (self.translatesAutoresizingMaskIntoConstraints) {
  53. self.translatesAutoresizingMaskIntoConstraints = NO;
  54. }
  55. if (width > 0) {
  56. [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:width]];
  57. }
  58. if (height > 0) {
  59. [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:height]];
  60. }
  61. }
  62. - (void)scott_addConstraintEqualWithView:(UIView *)view widthToView:(UIView *)wView heightToView:(UIView *)hView {
  63. if (view.translatesAutoresizingMaskIntoConstraints) {
  64. view.translatesAutoresizingMaskIntoConstraints = NO;
  65. }
  66. if (wView) {
  67. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:wView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
  68. }
  69. if (hView) {
  70. [self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:hView attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];
  71. }
  72. }
  73. - (NSLayoutConstraint *)scott_addConstraintCenterXToView:(UIView *)xView constant:(CGFloat)constant {
  74. if (xView.translatesAutoresizingMaskIntoConstraints) {
  75. xView.translatesAutoresizingMaskIntoConstraints = NO;
  76. }
  77. NSLayoutConstraint *centerXConstraint = [NSLayoutConstraint constraintWithItem:xView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:constant];
  78. [self addConstraint:centerXConstraint];
  79. return centerXConstraint;
  80. }
  81. - (NSLayoutConstraint *)scott_addConstraintCenterYToView:(UIView *)yView constant:(CGFloat)constant {
  82. if (yView.translatesAutoresizingMaskIntoConstraints) {
  83. yView.translatesAutoresizingMaskIntoConstraints = NO;
  84. }
  85. NSLayoutConstraint *centerYConstraint = [NSLayoutConstraint constraintWithItem:yView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:constant];
  86. [self addConstraint:centerYConstraint];
  87. return centerYConstraint;
  88. }
  89. - (void)scott_removeConstraintWithAttribte:(NSLayoutAttribute)attr {
  90. for (NSLayoutConstraint *constraint in self.constraints) {
  91. if (constraint.firstAttribute == attr) {
  92. [self removeConstraint:constraint];
  93. break;
  94. }
  95. }
  96. }
  97. - (void)scott_removeConstraintWithView:(UIView *)view attribute:(NSLayoutAttribute)attr {
  98. for (NSLayoutConstraint *constraint in self.constraints) {
  99. if (constraint.firstAttribute == attr && constraint.firstItem == view) {
  100. [self removeConstraint:constraint];
  101. break;
  102. }
  103. }
  104. }
  105. - (void)scott_removeAllConstraints {
  106. [self removeConstraints:self.constraints];
  107. }
  108. @end