Handle translucent status bar within Expo automatically.

- Escape hatch of `ReactNavigation.SafeAreaView.setStatusBarHeight()`.
- `if (Platform.OS === 'android') { ReactNavigation.SafeAreaView.setStatusBarHeight(0) }` to revert to old behavior.
This commit is contained in:
Brent Vatne 2018-01-12 15:34:40 -08:00
parent 6ac3bb90ed
commit 7fa0ef3aee
3 changed files with 37 additions and 9 deletions

View File

@ -52,7 +52,26 @@ const isIPad = (() => {
return true;
})();
let _customStatusBarHeight = null;
const statusBarHeight = isLandscape => {
if (_customStatusBarHeight !== null) {
return _customStatusBarHeight;
}
/**
* This is a temporary workaround because we don't have a way to detect
* if the status bar is translucent or opaque. If opaque, we don't need to
* factor in the height here; if translucent (content renders under it) then
* we do.
*/
if (Platform.OS === 'android') {
if (global.Expo) {
return global.Expo.Constants.statusBarHeight;
} else {
return 0;
}
}
if (isIPhoneX) {
return isLandscape ? 0 : 44;
}
@ -77,6 +96,10 @@ const doubleFromPercentString = percent => {
};
class SafeView extends Component {
static setStatusBarHeight = height => {
_customStatusBarHeight = height;
};
state = {
touchesTop: true,
touchesBottom: true,
@ -100,10 +123,6 @@ class SafeView extends Component {
render() {
const { forceInset = false, isLandscape, children, style } = this.props;
if (Platform.OS !== 'ios') {
return <Animated.View style={style}>{this.props.children}</Animated.View>;
}
const safeAreaStyle = this._getSafeAreaStyle();
return (

View File

@ -4,6 +4,7 @@ import * as React from 'react';
import { Animated, StyleSheet } from 'react-native';
import { TabBar } from 'react-native-tab-view';
import TabBarIcon from './TabBarIcon';
import SafeAreaView from '../SafeAreaView';
import type {
NavigationAction,
@ -24,6 +25,7 @@ type Props = {
upperCaseLabel: boolean,
allowFontScaling: boolean,
position: Animated.Value,
tabBarPosition: string,
navigation: NavigationScreenProp<NavigationState>,
jumpToIndex: (index: number) => void,
getLabel: (scene: TabScene) => ?(React.Node | string),
@ -53,6 +55,7 @@ export default class TabBarTop extends React.PureComponent<Props> {
_renderLabel = (scene: TabScene) => {
const {
position,
tabBarPosition,
navigation,
activeTintColor,
inactiveTintColor,
@ -81,12 +84,16 @@ export default class TabBarTop extends React.PureComponent<Props> {
const label = this.props.getLabel({ ...scene, tintColor });
if (typeof label === 'string') {
return (
<Animated.Text
style={[styles.label, { color }, labelStyle]}
allowFontScaling={allowFontScaling}
<SafeAreaView
forceInset={{ top: tabBarPosition === 'top' ? 'always' : 'never' }}
>
{upperCaseLabel ? label.toUpperCase() : label}
</Animated.Text>
<Animated.Text
style={[styles.label, { color }, labelStyle]}
allowFontScaling={allowFontScaling}
>
{upperCaseLabel ? label.toUpperCase() : label}
</Animated.Text>
</SafeAreaView>
);
}
if (typeof label === 'function') {

View File

@ -145,10 +145,12 @@ class TabView extends React.PureComponent<Props> {
if (typeof TabBarComponent === 'undefined') {
return null;
}
return (
<TabBarComponent
{...props}
{...tabBarOptions}
tabBarPosition={this.props.tabBarPosition}
screenProps={this.props.screenProps}
navigation={this.props.navigation}
getLabel={this._getLabel}