使用React Navigation的React Native固定页脚

时间:2018-09-09 17:01:31

标签: javascript reactjs react-native ecmascript-6 react-navigation

我正在使用"react-navigation": "^2.11.2",有一个TabNavigator(),带有3个标签:A,B和C。

所以我用:

...
_Profile: {
  screen: TabNavigator(
    {
      First: A,
      Second: B,
      Third: C
    },
    {
      tabBarPosition: "top",
      swipeEnabled: true,
      lazy: false
    }
  ),
  navigationOptions: ({ navigation }) => ({
    header: <ProfileHeader navigation={navigation} />
  })
},
...

我想在页面A和B中有一个固定的页脚,但在页面C中没有。

首先,我尝试在每个A和B中创建一个页脚,但结果与我想要的有所不同,请参见下图:

The Screen A

但是当我尝试滑动到选项卡B时,您会看到页脚未固定:

While I'm swiping from tab A to B

对此有任何想法吗?

  

谢谢!

1 个答案:

答案 0 :(得分:-1)

我问了贡献者,从现在开始我们有一个完整的例子:

  

带有页脚的自定义标签:

     

Github example


更新

我猜链接已断开,所以我将代码粘贴到这里:

import React from "react";
import {
  LayoutAnimation,
  View,
  StyleSheet,
  StatusBar,
  Text
} from "react-native";
import { SafeAreaView, createMaterialTopTabNavigator } from "react-navigation";
import Ionicons from "react-native-vector-icons/Ionicons";
import { Button } from "./commonComponents/ButtonWithMargin";

class MyHomeScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: "Home",
    tabBarIcon: ({ tintColor, focused, horizontal }) => (
      <Ionicons
        name={focused ? "ios-home" : "ios-home"}
        size={horizontal ? 20 : 26}
        style={{ color: tintColor }}
      />
    )
  };
  render() {
    const { navigation } = this.props;
    return (
      <SafeAreaView forceInset={{ horizontal: "always", top: "always" }}>
        <Text>Home Screen</Text>
        <Button
          onPress={() => navigation.navigate("Home")}
          title="Go to home tab"
        />
        <Button onPress={() => navigation.goBack(null)} title="Go back" />
      </SafeAreaView>
    );
  }
}

class RecommendedScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: "Recommended",
    tabBarIcon: ({ tintColor, focused, horizontal }) => (
      <Ionicons
        name={focused ? "ios-people" : "ios-people"}
        size={horizontal ? 20 : 26}
        style={{ color: tintColor }}
      />
    )
  };
  render() {
    const { navigation } = this.props;
    return (
      <SafeAreaView forceInset={{ horizontal: "always", top: "always" }}>
        <Text>Recommended Screen</Text>
        <Button
          onPress={() => navigation.navigate("Home")}
          title="Go to home tab"
        />
        <Button onPress={() => navigation.goBack(null)} title="Go back" />
      </SafeAreaView>
    );
  }
}

class FeaturedScreen extends React.Component {
  static navigationOptions = ({ navigation }) => ({
    tabBarLabel: "Featured",
    tabBarIcon: ({ tintColor, focused, horizontal }) => (
      <Ionicons
        name={focused ? "ios-star" : "ios-star"}
        size={horizontal ? 20 : 26}
        style={{ color: tintColor }}
      />
    )
  });
  render() {
    const { navigation } = this.props;
    return (
      <SafeAreaView forceInset={{ horizontal: "always", top: "always" }}>
        <Text>Featured Screen</Text>
        <Button
          onPress={() => navigation.navigate("Home")}
          title="Go to home tab"
        />
        <Button onPress={() => navigation.goBack(null)} title="Go back" />
      </SafeAreaView>
    );
  }
}

const SimpleTabs = createMaterialTopTabNavigator({
  Home: MyHomeScreen,
  Recommended: RecommendedScreen,
  Featured: FeaturedScreen
});

class TabNavigator extends React.Component {
  static router = SimpleTabs.router;
  componentWillUpdate() {
    LayoutAnimation.easeInEaseOut();
  }
  render() {
    const { navigation } = this.props;
    const { routes, index } = navigation.state;
    const activeRoute = routes[index];
    let bottom = null;
    if (activeRoute.routeName !== "Home") {
      bottom = (
        <View style={{ height: 50, borderTopWidth: StyleSheet.hairlineWidth }}>
          <Button title="Check out" onPress={() => {}} />
        </View>
      );
    }
    return (
      <View style={{ flex: 1 }}>
        <StatusBar barStyle="default" />
        <SafeAreaView
          style={{ flex: 1 }}
          forceInset={{ horizontal: "always", top: "always" }}
        >
          <SimpleTabs navigation={navigation} />
        </SafeAreaView>
        {bottom}
      </View>
    );
  }
}

export default TabNavigator;