Sin descripción

RNCSafeAreaView.m 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Simplified version of https://github.com/facebook/react-native/blob/master/React/Views/SafeAreaView/RCTSafeAreaView.m
  2. #import "RNCSafeAreaView.h"
  3. #import <React/RCTBridge.h>
  4. #import <React/RCTUIManager.h>
  5. @implementation RNCSafeAreaView
  6. - (BOOL)isSupportedByOS
  7. {
  8. return [self respondsToSelector:@selector(safeAreaInsets)];
  9. }
  10. - (UIEdgeInsets)realOrEmulateSafeAreaInsets
  11. {
  12. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  13. if (self.isSupportedByOS) {
  14. if (@available(iOS 11.0, *)) {
  15. return self.safeAreaInsets;
  16. }
  17. }
  18. #endif
  19. return self.emulatedSafeAreaInsets;
  20. }
  21. - (UIEdgeInsets)emulatedSafeAreaInsets
  22. {
  23. UIViewController* vc = self.reactViewController;
  24. if (!vc) {
  25. return UIEdgeInsetsZero;
  26. }
  27. CGFloat topLayoutOffset = vc.topLayoutGuide.length;
  28. CGFloat bottomLayoutOffset = vc.bottomLayoutGuide.length;
  29. CGRect safeArea = vc.view.bounds;
  30. safeArea.origin.y += topLayoutOffset;
  31. safeArea.size.height -= topLayoutOffset + bottomLayoutOffset;
  32. CGRect localSafeArea = [vc.view convertRect:safeArea toView:self];
  33. UIEdgeInsets safeAreaInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  34. if (CGRectGetMinY(localSafeArea) > CGRectGetMinY(self.bounds)) {
  35. safeAreaInsets.top = CGRectGetMinY(localSafeArea) - CGRectGetMinY(self.bounds);
  36. }
  37. if (CGRectGetMaxY(localSafeArea) < CGRectGetMaxY(self.bounds)) {
  38. safeAreaInsets.bottom = CGRectGetMaxY(self.bounds) - CGRectGetMaxY(localSafeArea);
  39. }
  40. return safeAreaInsets;
  41. }
  42. - (void)safeAreaInsetsDidChange
  43. {
  44. [self invalidateSafeAreaInsets];
  45. }
  46. - (void)invalidateSafeAreaInsets
  47. {
  48. UIEdgeInsets safeAreaInsets = [self realOrEmulateSafeAreaInsets];
  49. self.onInsetsChange(@{
  50. @"insets": @{
  51. @"top": @(safeAreaInsets.top),
  52. @"right": @(safeAreaInsets.right),
  53. @"bottom": @(safeAreaInsets.bottom),
  54. @"left": @(safeAreaInsets.left),
  55. }
  56. });
  57. }
  58. - (void)layoutSubviews
  59. {
  60. [super layoutSubviews];
  61. if (!self.isSupportedByOS) {
  62. [self invalidateSafeAreaInsets];
  63. }
  64. }
  65. @end