react-native - react 原生不重叠状态栏

标签 react-native statusbar overlapping

我正在学习 React Native,但我不明白如何让所有内容不与状态栏重叠。

this is how it looks like

I have tried
translucent={true/flase}
hidden

最佳答案

这是预期的行为,一种解决方案是在您的顶级 View 中添加一个与状态栏当前高度相等的填充或具有 StatusBarView与状态栏高度相同。

引用这个插件https://github.com/jgkim/react-native-status-bar-size用于监听状态栏高度变化。

例如

import _ from 'lodash';
import React, { Component } from 'react';
import { View, StatusBar} from 'react-native';
import StatusBarSizeIOS from 'react-native-status-bar-size';

const STATUS_BAR_EXTRA_PADDING = 2;
const DEFAULT_STATUS_BAR_HEIGHT = 10;

class StatusBarView extends Component {
  state = { 
    statusBarHeight: _.get(StatusBarSizeIOS, 'currentHeight', DEFAULT_STATUS_BAR_HEIGHT);
  }  

  _handleStatusBarSizeDidChange = (statusBarHeight) => this.setState({ statusBarHeight })

  componentDidMount() {
    StatusBarSizeIOS.addEventListener('didChange', this._handleStatusBarSizeDidChange);
  }

  render() {
    return (
      <View
        style={{
          height: this.state.statusBarHeight + STATUS_BAR_EXTRA_PADDING,
        }}
      >
        <StatusBar {...this.props} />
      </View>
    );
  }
}

export default StatusBarView;

现在在你的屏幕里面你可以做类似的事情
import StatusBarView from '{view_path}';
...
render() {
  return (
    <View>
       <StatusBarView barStyle="default" />
       <View>{rest of the view}</View>
    </View>
  );
}

关于react-native - react 原生不重叠状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45520281/

相关文章:

reactjs - React native 中的未知模块 "Firebase"

bash - 如何在状态栏中显示 tmux 环境变量(作为窗口格式)

android - 自定义状态栏

javascript - 在 render() 函数中定义函数组件是反模式吗?

react-native - reactnative - flatlist - 使用滚动条滚动

javascript - 如何从绑定(bind)到 React Native 中的 flatlist 的不同组件调用函数

swift - 点击设备时如何隐藏状态栏和导航栏

algorithm - 子集选择的贪婪或动态算法

r - 比较同一数据框中的日期间隔

r - GenomicRanges 包中重叠段的宽度