簡體   English   中英

如何在 React Native 中將圖像添加到導航抽屜的標題?

[英]How to add an image to Header of Navigation Drawer in React Native?

我正在嘗試將公司徽標添加到 Drawer Navigator 的標題中,但它僅顯示屏幕的標題。 這是我的代碼:

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, ScrollView, Dimensions, Image} from 'react-native';
import { createDrawerNavigator, createAppContainer, DrawerItems, SafeAreaView } from 'react-navigation';
import HomeScreen from './screens/HomeScreen';
import SettingsScreen from './screens/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);

"dependencies": {
        "native-base": "^2.10.0",
        "react": "16.6.3",
        "react-native": "0.55.2",
        "react-native-gesture-handler": "^1.0.12",
        "react-navigation": "^3.0.9"
      }

只需將 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>
   )
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM