UIView+React.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "UIView+React.h"
  8. #import <objc/runtime.h>
  9. #import "RCTAssert.h"
  10. #import "RCTLog.h"
  11. #import "RCTShadowView.h"
  12. @implementation UIView (React)
  13. - (NSNumber *)reactTag
  14. {
  15. return objc_getAssociatedObject(self, _cmd);
  16. }
  17. - (void)setReactTag:(NSNumber *)reactTag
  18. {
  19. objc_setAssociatedObject(self, @selector(reactTag), reactTag, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  20. }
  21. - (NSNumber *)nativeID
  22. {
  23. return objc_getAssociatedObject(self, _cmd);
  24. }
  25. - (void)setNativeID:(NSNumber *)nativeID
  26. {
  27. objc_setAssociatedObject(self, @selector(nativeID), nativeID, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  28. }
  29. - (BOOL)shouldAccessibilityIgnoresInvertColors
  30. {
  31. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  32. if (@available(iOS 11.0, *)) {
  33. return self.accessibilityIgnoresInvertColors;
  34. }
  35. #endif
  36. return NO;
  37. }
  38. - (void)setShouldAccessibilityIgnoresInvertColors:(BOOL)shouldAccessibilityIgnoresInvertColors
  39. {
  40. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  41. if (@available(iOS 11.0, *)) {
  42. self.accessibilityIgnoresInvertColors = shouldAccessibilityIgnoresInvertColors;
  43. }
  44. #endif
  45. }
  46. - (BOOL)isReactRootView
  47. {
  48. return RCTIsReactRootView(self.reactTag);
  49. }
  50. - (NSNumber *)reactTagAtPoint:(CGPoint)point
  51. {
  52. UIView *view = [self hitTest:point withEvent:nil];
  53. while (view && !view.reactTag) {
  54. view = view.superview;
  55. }
  56. return view.reactTag;
  57. }
  58. - (NSArray<UIView *> *)reactSubviews
  59. {
  60. return objc_getAssociatedObject(self, _cmd);
  61. }
  62. - (UIView *)reactSuperview
  63. {
  64. return self.superview;
  65. }
  66. - (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
  67. {
  68. // We access the associated object directly here in case someone overrides
  69. // the `reactSubviews` getter method and returns an immutable array.
  70. NSMutableArray *subviews = objc_getAssociatedObject(self, @selector(reactSubviews));
  71. if (!subviews) {
  72. subviews = [NSMutableArray new];
  73. objc_setAssociatedObject(self, @selector(reactSubviews), subviews, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  74. }
  75. [subviews insertObject:subview atIndex:atIndex];
  76. }
  77. - (void)removeReactSubview:(UIView *)subview
  78. {
  79. // We access the associated object directly here in case someone overrides
  80. // the `reactSubviews` getter method and returns an immutable array.
  81. NSMutableArray *subviews = objc_getAssociatedObject(self, @selector(reactSubviews));
  82. [subviews removeObject:subview];
  83. [subview removeFromSuperview];
  84. }
  85. #pragma mark - Display
  86. - (YGDisplay)reactDisplay
  87. {
  88. return self.isHidden ? YGDisplayNone : YGDisplayFlex;
  89. }
  90. - (void)setReactDisplay:(YGDisplay)display
  91. {
  92. self.hidden = display == YGDisplayNone;
  93. }
  94. #pragma mark - Layout Direction
  95. - (UIUserInterfaceLayoutDirection)reactLayoutDirection
  96. {
  97. if ([self respondsToSelector:@selector(semanticContentAttribute)]) {
  98. return [UIView userInterfaceLayoutDirectionForSemanticContentAttribute:self.semanticContentAttribute];
  99. } else {
  100. return [objc_getAssociatedObject(self, @selector(reactLayoutDirection)) integerValue];
  101. }
  102. }
  103. - (void)setReactLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection
  104. {
  105. if ([self respondsToSelector:@selector(setSemanticContentAttribute:)]) {
  106. self.semanticContentAttribute =
  107. layoutDirection == UIUserInterfaceLayoutDirectionLeftToRight ?
  108. UISemanticContentAttributeForceLeftToRight :
  109. UISemanticContentAttributeForceRightToLeft;
  110. } else {
  111. objc_setAssociatedObject(self, @selector(reactLayoutDirection), @(layoutDirection), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  112. }
  113. }
  114. #pragma mark - zIndex
  115. - (NSInteger)reactZIndex
  116. {
  117. return self.layer.zPosition;
  118. }
  119. - (void)setReactZIndex:(NSInteger)reactZIndex
  120. {
  121. self.layer.zPosition = reactZIndex;
  122. }
  123. - (NSArray<UIView *> *)reactZIndexSortedSubviews
  124. {
  125. // Check if sorting is required - in most cases it won't be.
  126. BOOL sortingRequired = NO;
  127. for (UIView *subview in self.subviews) {
  128. if (subview.reactZIndex != 0) {
  129. sortingRequired = YES;
  130. break;
  131. }
  132. }
  133. return sortingRequired ? [self.reactSubviews sortedArrayUsingComparator:^NSComparisonResult(UIView *a, UIView *b) {
  134. if (a.reactZIndex > b.reactZIndex) {
  135. return NSOrderedDescending;
  136. } else {
  137. // Ensure sorting is stable by treating equal zIndex as ascending so
  138. // that original order is preserved.
  139. return NSOrderedAscending;
  140. }
  141. }] : self.subviews;
  142. }
  143. - (void)didUpdateReactSubviews
  144. {
  145. for (UIView *subview in self.reactSubviews) {
  146. [self addSubview:subview];
  147. }
  148. }
  149. - (void)didSetProps:(__unused NSArray<NSString *> *)changedProps
  150. {
  151. // The default implementation does nothing.
  152. }
  153. - (void)reactSetFrame:(CGRect)frame
  154. {
  155. // These frames are in terms of anchorPoint = topLeft, but internally the
  156. // views are anchorPoint = center for easier scale and rotation animations.
  157. // Convert the frame so it works with anchorPoint = center.
  158. CGPoint position = {CGRectGetMidX(frame), CGRectGetMidY(frame)};
  159. CGRect bounds = {CGPointZero, frame.size};
  160. // Avoid crashes due to nan coords
  161. if (isnan(position.x) || isnan(position.y) ||
  162. isnan(bounds.origin.x) || isnan(bounds.origin.y) ||
  163. isnan(bounds.size.width) || isnan(bounds.size.height)) {
  164. RCTLogError(@"Invalid layout for (%@)%@. position: %@. bounds: %@",
  165. self.reactTag, self, NSStringFromCGPoint(position), NSStringFromCGRect(bounds));
  166. return;
  167. }
  168. self.center = position;
  169. self.bounds = bounds;
  170. }
  171. - (UIViewController *)reactViewController
  172. {
  173. id responder = [self nextResponder];
  174. while (responder) {
  175. if ([responder isKindOfClass:[UIViewController class]]) {
  176. return responder;
  177. }
  178. responder = [responder nextResponder];
  179. }
  180. return nil;
  181. }
  182. - (void)reactAddControllerToClosestParent:(UIViewController *)controller
  183. {
  184. if (!controller.parentViewController) {
  185. UIView *parentView = (UIView *)self.reactSuperview;
  186. while (parentView) {
  187. if (parentView.reactViewController) {
  188. [parentView.reactViewController addChildViewController:controller];
  189. [controller didMoveToParentViewController:parentView.reactViewController];
  190. break;
  191. }
  192. parentView = (UIView *)parentView.reactSuperview;
  193. }
  194. return;
  195. }
  196. }
  197. /**
  198. * Focus manipulation.
  199. */
  200. - (BOOL)reactIsFocusNeeded
  201. {
  202. return [(NSNumber *)objc_getAssociatedObject(self, @selector(reactIsFocusNeeded)) boolValue];
  203. }
  204. - (void)setReactIsFocusNeeded:(BOOL)isFocusNeeded
  205. {
  206. objc_setAssociatedObject(self, @selector(reactIsFocusNeeded), @(isFocusNeeded), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  207. }
  208. - (void)reactFocus {
  209. if (![self becomeFirstResponder]) {
  210. self.reactIsFocusNeeded = YES;
  211. }
  212. }
  213. - (void)reactFocusIfNeeded {
  214. if (self.reactIsFocusNeeded) {
  215. if ([self becomeFirstResponder]) {
  216. self.reactIsFocusNeeded = NO;
  217. }
  218. }
  219. }
  220. - (void)reactBlur {
  221. [self resignFirstResponder];
  222. }
  223. #pragma mark - Layout
  224. - (UIEdgeInsets)reactBorderInsets
  225. {
  226. CGFloat borderWidth = self.layer.borderWidth;
  227. return UIEdgeInsetsMake(borderWidth, borderWidth, borderWidth, borderWidth);
  228. }
  229. - (UIEdgeInsets)reactPaddingInsets
  230. {
  231. return UIEdgeInsetsZero;
  232. }
  233. - (UIEdgeInsets)reactCompoundInsets
  234. {
  235. UIEdgeInsets borderInsets = self.reactBorderInsets;
  236. UIEdgeInsets paddingInsets = self.reactPaddingInsets;
  237. return UIEdgeInsetsMake(
  238. borderInsets.top + paddingInsets.top,
  239. borderInsets.left + paddingInsets.left,
  240. borderInsets.bottom + paddingInsets.bottom,
  241. borderInsets.right + paddingInsets.right
  242. );
  243. }
  244. - (CGRect)reactContentFrame
  245. {
  246. return UIEdgeInsetsInsetRect(self.bounds, self.reactCompoundInsets);
  247. }
  248. #pragma mark - Accessiblity
  249. - (UIView *)reactAccessibilityElement
  250. {
  251. return self;
  252. }
  253. @end