React原生标签导航屏幕标题未显示

时间:2018-05-25 19:22:47

标签: reactjs react-native

我创建了2个使用createStackNavigator和createBottomTabNavigator的标签,下面是截图

enter image description here

我的App.js代码位于

之下
import React from 'react';
import { StatusBar, View, Text } from 'react-native';
import MainNavigator from './src/navigation';

export default class App extends React.Component {
  render() {
    return (
        <View style={{ flex:1 }}>
            <StatusBar backgroundColor="#000000" barStyle="light-content"/>
            <MainNavigator/>
        </View>
    );
  }
}
包含在App.js中的

和src / navigation / index.js如下所示

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  StatusBar,
  Text,
  View
} from 'react-native';
import { createBottomTabNavigator, createStackNavigator } from 'react-navigation';
import { Icon } from 'react-native-elements';
import Colors from '../constants/Colors';

import Splash from '../components/Splash/Splash';
import Login from '../components/Login/Login';
import Home from '../components/Home/Home';
import Profile from '../components/Profile/Profile';

const MainNavigator = createStackNavigator({
    // Splash: {screen: Splash, navigationOptions:{ header: null, title: 'Splash'}},
    // Login: {screen: Login, navigationOptions:{ header: null, title: 'Login'}},
    // Home: {screen: Home, navigationOptions:{ title: 'Home'}}
    Root: {
        screen: createBottomTabNavigator({
            home: {
              screen: Home,
              navigationOptions: {
                title: 'Home',
                headerTitle: 'Home',
                tabBarLabel: 'Home',
                tabBarIcon: ({ tintColor, focused }) => (
                  <Icon
                  name={'home'}
                  size={24}
                  color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
                  />
                ),
              }
            },
            profile: {
              screen: Profile,
              navigationOptions: {
                tabBarLabel: 'Profile',
                tabBarIcon: ({ tintColor, focused }) => (
                  <Icon
                  name={'account-circle'}
                  size={24}
                  color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
                  />
                ),
              }
            }
        },
        {
          tabBarOptions: {
            activeTintColor: Colors.tintColor,
            showLabel: true,
            showIcon: true,
            indicatorStyle: {
              backgroundColor: 'transparent'
            },
            iconStyle: {
              width: 24,
              height: 24
            },
            style: {
              backgroundColor: '#eeeeee',
            },
            tabBarIcon: ({ tintColor }) => {Colors.darkTintColor},
          },
          lazy: true,
          tabBarPosition: 'bottom',
        }),
    }
});

export default MainNavigator;

我想要显示标题,我尝试了不同的方法,但没有奏效。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

createBottomTabNavigator 设置 navigationOptions 。 这是完整的代码:

const TabNav = createBottomTabNavigator(
  {
    home: {
      screen: Home,
      navigationOptions: {
        title: 'Home',
        headerTitle: 'Home',
        tabBarLabel: 'Home',
        tabBarIcon: ({ tintColor, focused }) => (
          <Icon
            name={'home'}
            size={24}
            color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
          />
        ),
      }
    },
    profile: {
      screen: Profile,
      navigationOptions: {
        tabBarLabel: 'Profile',
        tabBarIcon: ({ tintColor, focused }) => (
          <Icon
            name={'account-circle'}
            size={24}
            color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
          />
        ),
      }
    }
  },
  {
    tabBarPosition: 'bottom',
    animationEnabled: false,
    swipeEnabled: false,
  }
);

TabNav.navigationOptions = ({ navigation }) => {
  let { routeName } = navigation.state.routes[navigation.state.index];
  let title;
  if (routeName === 'home') {
    title = 'Home';
  } else if (routeName === 'profile') {
    title = 'Profile';
  }
  return {
    title,
  };
};

const MainNavigator = createStackNavigator({
  // Splash: {screen: Splash, navigationOptions:{ header: null, title: 'Splash'}},
  // Login: {screen: Login, navigationOptions:{ header: null, title: 'Login'}},
  // Home: {screen: Home, navigationOptions:{ title: 'Home'}}
  Root: {
    screen: TabNav
  }
});

export default MainNavigator;

答案 1 :(得分:0)

import {createBottomTabNavigator} from 'react-navigation-tabs';

//TODO Bottom tab
const BottomTabNav = createBottomTabNavigator(
  {
    home: {
      screen: Comments,
      navigationOptions: {
        title: 'Home',
        headerTitle: 'Home',
        tabBarLabel: 'Home',
        tabBarIcon: ({ tintColor, focused }) => (
          <Icon
            name={'home'}
            size={24}
            //color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
          />
        ),
      }
    },
    profile: {
      screen: PreviousAlert,
      navigationOptions: {
        tabBarLabel: 'Profile',
        tabBarIcon: ({ tintColor, focused }) => (
          <Icon
            name={'account-circle'}
            size={24}
//            color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
          />
        ),
      }
    }
  },
  {
    tabBarPosition: 'bottom',
    animationEnabled: false,
    swipeEnabled: false,
  }
);

BottomTabNav.navigationOptions = ({ navigation }) => {
  let { routeName } = navigation.state.routes[navigation.state.index];
  let title;
  if (routeName === 'home') {
    title = 'Home';
  } else if (routeName === 'profile') {
    title = 'Profile';
  }
  return {
    title,
  };
};
//TODO Bottom tab


const AppStack = createStackNavigator(
    {
        PreviousAlertScreen: {
            screen: BottomTabNav
        }
    },
);

Bottom Tab