RCTSafeAreaView.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "RCTSafeAreaView.h"
  8. #import <React/RCTBridge.h>
  9. #import <React/RCTUIManager.h>
  10. #import "RCTSafeAreaViewLocalData.h"
  11. @implementation RCTSafeAreaView {
  12. __weak RCTBridge *_bridge;
  13. UIEdgeInsets _currentSafeAreaInsets;
  14. }
  15. - (instancetype)initWithBridge:(RCTBridge *)bridge
  16. {
  17. if (self = [super initWithFrame:CGRectZero]) {
  18. _bridge = bridge;
  19. _emulateUnlessSupported = YES; // The default.
  20. }
  21. return self;
  22. }
  23. RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)decoder)
  24. RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
  25. - (NSString *)description
  26. {
  27. NSString *superDescription = [super description];
  28. // Cutting the last `>` character.
  29. if (superDescription.length > 0 && [superDescription characterAtIndex:superDescription.length - 1] == '>') {
  30. superDescription = [superDescription substringToIndex:superDescription.length - 1];
  31. }
  32. return [NSString stringWithFormat:@"%@; safeAreaInsets = %@; appliedSafeAreaInsets = %@>",
  33. superDescription,
  34. NSStringFromUIEdgeInsets([self safeAreaInsetsIfSupportedAndEnabled]),
  35. NSStringFromUIEdgeInsets(_currentSafeAreaInsets)];
  36. }
  37. - (BOOL)isSupportedByOS
  38. {
  39. return [self respondsToSelector:@selector(safeAreaInsets)];
  40. }
  41. - (UIEdgeInsets)safeAreaInsetsIfSupportedAndEnabled
  42. {
  43. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  44. if (self.isSupportedByOS) {
  45. if (@available(iOS 11.0, *)) {
  46. return self.safeAreaInsets;
  47. }
  48. }
  49. #endif
  50. return self.emulateUnlessSupported ? self.emulatedSafeAreaInsets : UIEdgeInsetsZero;
  51. }
  52. - (UIEdgeInsets)emulatedSafeAreaInsets
  53. {
  54. UIViewController *vc = self.reactViewController;
  55. if (!vc) {
  56. return UIEdgeInsetsZero;
  57. }
  58. CGFloat topLayoutOffset = vc.topLayoutGuide.length;
  59. CGFloat bottomLayoutOffset = vc.bottomLayoutGuide.length;
  60. CGRect safeArea = vc.view.bounds;
  61. safeArea.origin.y += topLayoutOffset;
  62. safeArea.size.height -= topLayoutOffset + bottomLayoutOffset;
  63. CGRect localSafeArea = [vc.view convertRect:safeArea toView:self];
  64. UIEdgeInsets safeAreaInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  65. if (CGRectGetMinY(localSafeArea) > CGRectGetMinY(self.bounds)) {
  66. safeAreaInsets.top = CGRectGetMinY(localSafeArea) - CGRectGetMinY(self.bounds);
  67. }
  68. if (CGRectGetMaxY(localSafeArea) < CGRectGetMaxY(self.bounds)) {
  69. safeAreaInsets.bottom = CGRectGetMaxY(self.bounds) - CGRectGetMaxY(localSafeArea);
  70. }
  71. return safeAreaInsets;
  72. }
  73. static BOOL UIEdgeInsetsEqualToEdgeInsetsWithThreshold(UIEdgeInsets insets1, UIEdgeInsets insets2, CGFloat threshold)
  74. {
  75. return ABS(insets1.left - insets2.left) <= threshold && ABS(insets1.right - insets2.right) <= threshold &&
  76. ABS(insets1.top - insets2.top) <= threshold && ABS(insets1.bottom - insets2.bottom) <= threshold;
  77. }
  78. - (void)safeAreaInsetsDidChange
  79. {
  80. [self invalidateSafeAreaInsets];
  81. }
  82. - (void)invalidateSafeAreaInsets
  83. {
  84. [self setSafeAreaInsets:self.safeAreaInsetsIfSupportedAndEnabled];
  85. }
  86. - (void)layoutSubviews
  87. {
  88. [super layoutSubviews];
  89. if (!self.isSupportedByOS && self.emulateUnlessSupported) {
  90. [self invalidateSafeAreaInsets];
  91. }
  92. }
  93. - (void)setSafeAreaInsets:(UIEdgeInsets)safeAreaInsets
  94. {
  95. if (UIEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
  96. return;
  97. }
  98. _currentSafeAreaInsets = safeAreaInsets;
  99. RCTSafeAreaViewLocalData *localData = [[RCTSafeAreaViewLocalData alloc] initWithInsets:safeAreaInsets];
  100. [_bridge.uiManager setLocalData:localData forView:self];
  101. }
  102. - (void)setEmulateUnlessSupported:(BOOL)emulateUnlessSupported
  103. {
  104. if (_emulateUnlessSupported == emulateUnlessSupported) {
  105. return;
  106. }
  107. _emulateUnlessSupported = emulateUnlessSupported;
  108. if ([self isSupportedByOS]) {
  109. return;
  110. }
  111. [self invalidateSafeAreaInsets];
  112. }
  113. @end