Skip to content

Commit

Permalink
[iOS] Add additionalSafeAreaInsets prop for SafeAreaView
Browse files Browse the repository at this point in the history
[iOS] Add additionalSafeAreaInsets prop for SafeAreaView

fix flow check

Add EdgeInsetsProp import
  • Loading branch information
zhongwuzw committed Mar 6, 2019
1 parent fe80a0c commit 92987ef
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ const requireNativeComponent = require('requireNativeComponent');

import type {ViewProps} from 'ViewPropTypes';
import type {NativeComponent} from 'ReactNative';
import type {EdgeInsetsProp} from 'EdgeInsetsPropType';

type NativeProps = $ReadOnly<{|
...ViewProps,
emulateUnlessSupported?: boolean,
additionalSafeAreaInsets?: ?EdgeInsetsProp,
|}>;

type RCTSafeAreaViewNativeType = Class<NativeComponent<NativeProps>>;
Expand Down
13 changes: 12 additions & 1 deletion Libraries/Components/SafeAreaView/SafeAreaView.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ const React = require('React');
const View = require('View');

import type {ViewProps} from 'ViewPropTypes';
import type {EdgeInsetsProp} from 'EdgeInsetsPropType';

type Props = $ReadOnly<{|
...ViewProps,
emulateUnlessSupported?: boolean,
|}>;

type IOSProps = $ReadOnly<{|
...Props,
/**
* Custom insets that you specify to modify safe area.
*
* @platform ios
*/
additionalSafeAreaInsets?: ?EdgeInsetsProp,
|}>;

let exported;

/**
Expand All @@ -39,7 +50,7 @@ if (Platform.OS === 'android') {
};
} else {
const RCTSafeAreaViewNativeComponent = require('RCTSafeAreaViewNativeComponent');
exported = class SafeAreaView extends React.Component<Props> {
exported = class SafeAreaView extends React.Component<IOSProps> {
render(): React.Node {
return (
<RCTSafeAreaViewNativeComponent
Expand Down
1 change: 1 addition & 0 deletions React/Views/SafeAreaView/RCTSafeAreaView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithBridge:(RCTBridge *)bridge;

@property (nonatomic, assign) BOOL emulateUnlessSupported;
@property (nonatomic, assign) UIEdgeInsets reactAdditionalSafeAreaInsets;

@end

Expand Down
23 changes: 18 additions & 5 deletions React/Views/SafeAreaView/RCTSafeAreaView.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ - (UIEdgeInsets)safeAreaInsetsIfSupportedAndEnabled
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
if (self.isSupportedByOS) {
if (@available(iOS 11.0, *)) {
return self.safeAreaInsets;
return [self totalSafeAreaInsetsWithInset:self.safeAreaInsets];
}
}
#endif
return self.emulateUnlessSupported ? self.emulatedSafeAreaInsets : UIEdgeInsetsZero;
return self.emulateUnlessSupported ? [self totalSafeAreaInsetsWithInset:self.emulatedSafeAreaInsets] : [self totalSafeAreaInsetsWithInset:UIEdgeInsetsZero];
}

- (UIEdgeInsets)emulatedSafeAreaInsets
Expand Down Expand Up @@ -99,15 +99,28 @@ - (void)layoutSubviews
}
}

- (UIEdgeInsets)totalSafeAreaInsetsWithInset:(UIEdgeInsets)insets
{
UIEdgeInsets additionalSafeAreaInsets = self.reactAdditionalSafeAreaInsets;
if (UIEdgeInsetsEqualToEdgeInsets(additionalSafeAreaInsets, UIEdgeInsetsZero)) {
return insets;
}
return UIEdgeInsetsMake(additionalSafeAreaInsets.top + insets.top,
additionalSafeAreaInsets.left + insets.left,
additionalSafeAreaInsets.bottom + insets.bottom,
additionalSafeAreaInsets.right + insets.right);
}

- (void)setSafeAreaInsets:(UIEdgeInsets)safeAreaInsets
{
if (UIEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
UIEdgeInsets totalSafeAreaInsets = [self totalSafeAreaInsetsWithInset:safeAreaInsets];
if (UIEdgeInsetsEqualToEdgeInsetsWithThreshold(totalSafeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
return;
}

_currentSafeAreaInsets = safeAreaInsets;
_currentSafeAreaInsets = totalSafeAreaInsets;

RCTSafeAreaViewLocalData *localData = [[RCTSafeAreaViewLocalData alloc] initWithInsets:safeAreaInsets];
RCTSafeAreaViewLocalData *localData = [[RCTSafeAreaViewLocalData alloc] initWithInsets:totalSafeAreaInsets];
[_bridge.uiManager setLocalData:localData forView:self];
}

Expand Down
1 change: 1 addition & 0 deletions React/Views/SafeAreaView/RCTSafeAreaViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ @implementation RCTSafeAreaViewManager
RCT_EXPORT_MODULE()

RCT_EXPORT_VIEW_PROPERTY(emulateUnlessSupported, BOOL)
RCT_REMAP_VIEW_PROPERTY(additionalSafeAreaInsets, reactAdditionalSafeAreaInsets, UIEdgeInsets)

- (UIView *)view
{
Expand Down

0 comments on commit 92987ef

Please sign in to comment.