UIScrollView+MJExtension.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // 代码地址: https://github.com/CoderMJLee/MJRefresh
  2. // UIScrollView+Extension.m
  3. // MJRefresh
  4. //
  5. // Created by MJ Lee on 14-5-28.
  6. // Copyright (c) 2014年 小码哥. All rights reserved.
  7. //
  8. #import "UIScrollView+MJExtension.h"
  9. #import <objc/runtime.h>
  10. #pragma clang diagnostic push
  11. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  12. @implementation UIScrollView (MJExtension)
  13. static BOOL respondsToAdjustedContentInset_;
  14. + (void)load
  15. {
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. respondsToAdjustedContentInset_ = [self instancesRespondToSelector:@selector(adjustedContentInset)];
  19. });
  20. }
  21. - (UIEdgeInsets)mj_inset
  22. {
  23. #ifdef __IPHONE_11_0
  24. if (respondsToAdjustedContentInset_) {
  25. return self.adjustedContentInset;
  26. }
  27. #endif
  28. return self.contentInset;
  29. }
  30. - (void)setMj_insetT:(CGFloat)mj_insetT
  31. {
  32. UIEdgeInsets inset = self.contentInset;
  33. inset.top = mj_insetT;
  34. #ifdef __IPHONE_11_0
  35. if (respondsToAdjustedContentInset_) {
  36. inset.top -= (self.adjustedContentInset.top - self.contentInset.top);
  37. }
  38. #endif
  39. self.contentInset = inset;
  40. }
  41. - (CGFloat)mj_insetT
  42. {
  43. return self.mj_inset.top;
  44. }
  45. - (void)setMj_insetB:(CGFloat)mj_insetB
  46. {
  47. UIEdgeInsets inset = self.contentInset;
  48. inset.bottom = mj_insetB;
  49. #ifdef __IPHONE_11_0
  50. if (respondsToAdjustedContentInset_) {
  51. inset.bottom -= (self.adjustedContentInset.bottom - self.contentInset.bottom);
  52. }
  53. #endif
  54. self.contentInset = inset;
  55. }
  56. - (CGFloat)mj_insetB
  57. {
  58. return self.mj_inset.bottom;
  59. }
  60. - (void)setMj_insetL:(CGFloat)mj_insetL
  61. {
  62. UIEdgeInsets inset = self.contentInset;
  63. inset.left = mj_insetL;
  64. #ifdef __IPHONE_11_0
  65. if (respondsToAdjustedContentInset_) {
  66. inset.left -= (self.adjustedContentInset.left - self.contentInset.left);
  67. }
  68. #endif
  69. self.contentInset = inset;
  70. }
  71. - (CGFloat)mj_insetL
  72. {
  73. return self.mj_inset.left;
  74. }
  75. - (void)setMj_insetR:(CGFloat)mj_insetR
  76. {
  77. UIEdgeInsets inset = self.contentInset;
  78. inset.right = mj_insetR;
  79. #ifdef __IPHONE_11_0
  80. if (respondsToAdjustedContentInset_) {
  81. inset.right -= (self.adjustedContentInset.right - self.contentInset.right);
  82. }
  83. #endif
  84. self.contentInset = inset;
  85. }
  86. - (CGFloat)mj_insetR
  87. {
  88. return self.mj_inset.right;
  89. }
  90. - (void)setMj_offsetX:(CGFloat)mj_offsetX
  91. {
  92. CGPoint offset = self.contentOffset;
  93. offset.x = mj_offsetX;
  94. self.contentOffset = offset;
  95. }
  96. - (CGFloat)mj_offsetX
  97. {
  98. return self.contentOffset.x;
  99. }
  100. - (void)setMj_offsetY:(CGFloat)mj_offsetY
  101. {
  102. CGPoint offset = self.contentOffset;
  103. offset.y = mj_offsetY;
  104. self.contentOffset = offset;
  105. }
  106. - (CGFloat)mj_offsetY
  107. {
  108. return self.contentOffset.y;
  109. }
  110. - (void)setMj_contentW:(CGFloat)mj_contentW
  111. {
  112. CGSize size = self.contentSize;
  113. size.width = mj_contentW;
  114. self.contentSize = size;
  115. }
  116. - (CGFloat)mj_contentW
  117. {
  118. return self.contentSize.width;
  119. }
  120. - (void)setMj_contentH:(CGFloat)mj_contentH
  121. {
  122. CGSize size = self.contentSize;
  123. size.height = mj_contentH;
  124. self.contentSize = size;
  125. }
  126. - (CGFloat)mj_contentH
  127. {
  128. return self.contentSize.height;
  129. }
  130. @end
  131. #pragma clang diagnostic pop