如何在反应导航中将标题放在标题上?

时间:2018-05-12 12:09:54

标签: react-native react-navigation

我正在使用反应导航。我想在屏幕的标题上显示抽屉。当我打开抽屉时,我的标题目前没有隐藏在抽屉下面。

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { createStackNavigator, createDrawerNavigator  } from 'react-navigation';
import CategoryScreen from './CategoryScreen';
import ProductsScreen from './ProductsScreen';
import CartScreen from './CartScreen';


const drawerScreens = createDrawerNavigator ({
    Category: CategoryScreen,
    Products: ProductsScreen,
},{
    initialRouteName: 'Category'
}
)


export default AppStack = createStackNavigator(
    { 
        drawer: {
            screen: drawerScreens,
            navigationOptions: ({ navigation }) => ({
                header: <View style={styles.container}><Text>Header</Text></View>
              }),
        }, 
        cart: {screen: CartScreen} 
    },
    {
        initialRouteName: 'drawer',
    }
);

const styles = StyleSheet.create({
    container: {
        height: 100,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'orange',
    }
})

那么如何显示抽屉导航抽屉重叠或覆盖的标题。

目前看起来像这样

enter image description here

3 个答案:

答案 0 :(得分:4)

  1. 您应该为CategoryScreenProductScreen
  2. 创建一个新的StackNavigator
  3. 您在CategoryScreenProductScreen导航选项
  4. 上设置标题

    这就是我的意思

    // wrap your screen inside the drawer into StackNavigator
    const CategoryNavigator = createStackNavigator({
      CategoryList: {
        screen: CategoryScreen,
        navigationOptions: {
          title: "Category",
          header: // any custom header here
        }
      },
    });
    
    const drawerScreens = createDrawerNavigator({
      Category: CategoryNavigator,
      Products: ProductNavigator,
    }, {
      initialRouteName: 'Category'
    })
    
    
    export default AppStack = createStackNavigator({
      drawer: {
        screen: drawerScreens,
      },
      cart: {
        screen: CartScreen
      }
    }, {
      initialRouteName: 'drawer',
    });

    这是结果

    Embedded StackNavigator

    以下将制作与您的屏幕截图类似的浮动标题

    将标题模式设置为float(您无需将CategoryScreenProductScreen包装到StackNavigator

    export default AppStack = createStackNavigator({
      drawer: {
        screen: drawerScreens,
      },
      cart: {
        screen: CartScreen
      }
    }, {
      headerMode: 'float', // set this header mode to float so you can share the header
      initialRouteName: 'drawer',
    });

    如果将标题模式更改为float,则会出现此结果 Float header

答案 1 :(得分:1)

在我的情况下,我制作自己的Header组件并在我想要的每个页面中使用它。它使我能够为每个页面自定义标题。

绝对是后门方式,我希望其他人能够准确回答你的问题。

这是一个例子......

主页:

export default class Home extends Component {
    render() {  
        return (
            <View style={{ flex: 1 }}>

                <Header showBorder={true}/>

                <ScrollView>
                   ...
                </ScrollView>
            </View>
        );
    }
}

标题组件:

export default class Header extends React.PureComponent {

  renderTitle = () => {
    if (this.props.title) {
      return (
        <View style={{ flexDirection: 'column', flex: 3, justifyContent: 'center' }}>
          <View style={{ alignSelf: 'flex-start' }}>
            <Text style={[styles.fontBold, { fontSize:17, color: colors.borderWidthColor }]}>{this.props.title}</Text>
          </View>
        </View>
      )
    }
  }

  renderBack = () => {
    if (this.props.back) {
      return (
        <View style={{ marginTop:3 }}>
          <TouchableOpacity
            onPress={() => {
              this.props.navigation.goBack()
            }}
            style={{ alignSelf: 'flex-start' }}>
            <Icon name="md-arrow-back" size={23} color="black" />
          </TouchableOpacity>
        </View>
      )
    }
  }


  render() {
    return (
      <View style={[{ height: 70, backgroundColor: this.props.backgroundColor, width: win.width, borderBottomWidth: this.props.showBorder ? 1 : 0 }]}>
        <View style={{ flex: 1, flexDirection: 'row', marginTop: Platform.OS == 'ios' ? 17 : 3 }}>
          <View style={{ flexDirection: 'column', flex: 1, justifyContent: 'center', marginLeft: 12 }}>
            {this.renderBack()}
            {this.renderTitle()}
          </View>
        </View>
      </View>
    )
  }
}

答案 2 :(得分:0)

希望这能帮到你……

           export function DrawerContent(props) {

               return(

                <View style={{flex:1}}>
               <DrawerContentScrollView {...props}>
               <View style={styles.drawerContent}>
               <View style={styles.userInfoSection}>
               <View style={{flexDirection:'row',marginTop: 15}}>
               <Avatar.Image size={50} />
               <View style={{marginLeft:15, flexDirection:'column'}}>
               <Title style={styles.title}>{status}</Title>
               <Caption style={styles.caption}>@_something</Caption>

                        </View>
                    </View>
                </View>
            </View>

        </DrawerContentScrollView>
      
    </View>
);
  }
         const styles = StyleSheet.create({
            userInfoSection: {
             paddingLeft: 20,
                         },
                          title: {
                fontSize: 16,
                marginTop: 3,
                  fontWeight: 'bold',
                   },
                   caption: {
                   fontSize: 14,
                   lineHeight: 14,
                     },

                 
                 });

在 Drawer 导航容器中的 App.js 中,我将 DrawerContent 作为道具传递并在 app.js main 中导入 DrawerContent 组件,如图所示

  const Navigation (){
      return(
  <Drawer.Navigator initialRouteName="Home"  drawerContent={props =><DrawerContent {...props} />}>
  <Drawer.Screen name="Home" component={HomeScreen} options={{marginLeft:5}} />
  <Drawer.Screen name="Notifications" component={NotificationsScreen} />
  </Drawer.Navigator>
  )
  }
     export default const App = ()=> {
                    return (
                           <NavigationContainer>
                              <Navigation />
                            </NavigationContainer>
               );
                   };