代码之家  ›  专栏  ›  技术社区  ›  MoKhajavi75

带React导航的React原生固定页脚

  •  0
  • MoKhajavi75  · 技术社区  · 8 年前

    我在用 "react-navigation": "^2.11.2" 去喝一杯 TabNavigator()

    所以我用:

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

    我想喝一杯 固定页脚 在A页和B页,但不在C页。

    The Screen A

    但当我尝试滑动到选项卡B时,您可以看到页脚没有固定:

    While I'm swiping from tab A to B

    1 回复  |  直到 8 年前
        1
  •  1
  •   MoKhajavi75    7 年前

    我问过投稿人,从现在开始我们有一个完整的例子:

    带页脚的自定义选项卡:

    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;