如何在React Native中将图像添加到导航抽屉的标题?

时间:2018-12-25 14:30:33

标签: javascript react-native react-native-android

我正在尝试将公司徽标添加到Drawer Navigator的标题中,但是它仅显示屏幕标题。这是我的代码:

从'react'导入React,{Component}; 从'react-native'导入{Platform,StyleSheet,Text,View,ScrollView,Dimensions,Image};     从“反应导航”导入{createDrawerNavigator,createAppContainer,DrawerItems,SafeAreaView};     从“ ./screens/HomeScreen”导入HomeScreen;     从“ ./screens/SettingsScreen”导入SettingsScreen;

class App extends Component {
  render() {
    return (
      <AppNavigator />
    );
  }
}

const AppNavigator = createDrawerNavigator({
  Home: {screen: HomeScreen},
  Settings: {screen: SettingsScreen},
},
{
  CustomDrawerContentComponent: props => (
    <SafeAreaView style={styles.container}>
        <View style={{height: 150, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center'}}>
          <Image source={require('./assets/typeme.png')} style={{height: 120, width:200}}></Image>
        </View>
      <ScrollView>
        <DrawerItems {...props} />
      </ScrollView>
    </SafeAreaView>
  )
}
);

const DrawerContent = (props) => (
  <View>
    <View
      style={{
        backgroundColor: '#f50057',
        height: 140,
        alignItems: 'center',
        justifyContent: 'center',
      }}
    >
      <Text style={{ color: 'white', fontSize: 30 }}>
        Header
      </Text>
    </View>
    <DrawerItems {...props} />
  </View>
)

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

export default createAppContainer(AppNavigator);

“依赖项”:{         “ native-base”:“ ^ 2.10.0”,         “反应”:“ 16.6.3”,         “ react-native”:“ 0.55.2”,         “ react-native-gesture-handler”:“ ^ 1.0.12”,         “反应导航”:“ ^ 3.0.9”       }

1 个答案:

答案 0 :(得分:1)

只需将const AppNavigator替换为

const AppNavigator = createDrawerNavigator({
  Home: {screen: HomeScreen},
  Settings: {screen: SettingsScreen},
}, {
   contentComponent: (props) => (
    <SafeAreaView style={styles.container}>
        <View style={{height: 100,alignItems: 'center', justifyContent: 'center'}}>

          <Text style={{fontSize: 32}}>LOGO</Text>
        </View>
      <ScrollView>
        <DrawerItems {...props} />
      </ScrollView>
    </SafeAreaView>
   )
});