【问题标题】:Add padding to a custom contentComponent for a DrawerNavigator to avoid overlaying the status bar为 DrawerNavigator 的自定义 contentComponent 添加填充以避免覆盖状态栏
【发布时间】:2019-05-23 21:08:56
【问题描述】:

当使用基本的 DrawerNavigator 时,有适当的 paddingTop 可以避免在状态栏上覆盖内容;但是,当添加自定义 contentComponent 时,填充不存在。代码:https://github.com/myplaceonline/testreactexpo/tree/drawermargin

Android 上没有自定义 contentComponent:

在 Android 上使用自定义 contentComponent:

我可以添加一些明确的边距,但我应该选择什么值?我已经在使用SafeAreaView,但我相信这仅适用于 iOS。

这是代码。评论 contentComponent: CustomDrawerContentComponent, 行以查看工作视图。

import React from 'react';
import { SafeAreaView, ScrollView, Text, View } from 'react-native';
import { createAppContainer, createDrawerNavigator, createStackNavigator, DrawerItems } from 'react-navigation';

class TestScreen extends React.Component {
  static navigationOptions = {
    title: "Test",
  };
  render() {
    return (
      <View style={{flex: 1, alignItems: "center", justifyContent: "center" }}>
        <Text>Test</Text>
      </View>
    );
  }
}

const CustomDrawerContentComponent = props => (
  <SafeAreaView style={{flex: 1}} forceInset={{ top: "always", horizontal: "never" }}>
    <ScrollView>
      <DrawerItems {...props} />
    </ScrollView>
  </SafeAreaView>
);

export default AppDrawer = createAppContainer(
  createDrawerNavigator(
    {
      DrawerTest: {
        screen: createStackNavigator(
          {
            Test: TestScreen
          },
        ),
        navigationOptions: {
          drawerLabel: "Test",
        }
      },
    },
    {
      contentComponent: CustomDrawerContentComponent,
    }
  )
);

<div data-snack-id="@git/github.com/myplaceonline/testreactexpo@drawermargin" data-snack-platform="ios" data-snack-preview="true" data-snack-theme="light" style="overflow:hidden;background:#fafafa;border:1px solid rgba(0,0,0,.08);border-radius:4px;height:505px;width:100%"></div>
<script async src="https://snack.expo.io/embed.js"></script>

【问题讨论】:

    标签: react-native react-navigation expo react-navigation-drawer


    【解决方案1】:

    解决方法:

    import Constants from 'expo-constants';
    
    <ScrollView style={{paddingTop: Constants.statusBarHeight}}>
    

    【讨论】:

    • 你是如何得到那个“常数”的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2020-11-06
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多