React Native Expo StackNavigator重叠通知栏

时间:2017-07-11 21:41:33

标签: android reactjs react-native expo create-react-native-app

我正在尝试为我的React Native Expo应用程序实现导航栏。这是一个问题:

enter image description here

"dependencies": {
    "expo": "^18.0.3",
    "react": "16.0.0-alpha.12",
    "react-native": "^0.45.1",
    "react-navigation": "^1.0.0-beta.11"
}

我不知道在哪里以及如何为此组件设置样式以使其不与通知栏重叠。我尝试为我的根视图设置marginTop: StatusBar.currentHeight,但它不起作用。它在视图上应用了边距,但未在导航栏上应用。

我的应用:

import {StackNavigator} from 'react-navigation';
import Home from './app/screens/Home';

export default App = StackNavigator({
  Home: { screen: Home }
})

主页:

export default class Home extends Component {
    constructor() {
        super();  
        // <...>
    }

    static navigationOptions = {
        title: 'Welcome'
    };

    // <...>
}

2 个答案:

答案 0 :(得分:22)

如果您将整个应用程序包装在具有上边距的View中,它将起作用。

示例:https://snack.expo.io/r1gb9TGH-

将来,我们将把它构建成反应导航,以便它自动发生。

import React, {Component} from 'react';
import {StatusBar, View} from 'react-native'
import {StackNavigator} from 'react-navigation';
import Home from './app/screens/Home';

const RootNavigator = StackNavigator({
  Home: { screen: Home }
})

export default class App extends Component {
  render() {
    return (
      <View style={{ flex: 1, marginTop: StatusBar.currentHeight }}>
        <RootNavigator />
      </View>
    )
  }
}

答案 1 :(得分:3)

将此行添加到app.json文件

"translucent": false

"androidStatusBar"

像这样:

"androidStatusBar": {
  "barStyle": "light-content",
  "backgroundColor": "#3a81fd",
  "hidden": false,
  "translucent": false
},

进一步了解:Configuring StatusBar Docs