javascript - React Native 固定页脚与 React 导航

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

我正在使用 "react-navigation": "^2.11.2" 并拥有一个带有 3 个选项卡的 TabNavigator():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

对此有什么想法吗?

Thanks in advance!

最佳答案

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

Custom Tabs with footer:

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;

关于javascript - React Native 固定页脚与 React 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52246769/

相关文章:

javascript - 删除div标签+ jquery下的类属性

javascript - 如何在没有 babel 的情况下动态创建 "compiled"React 组件?

react-native - 在React Native FlatList中渲染多个数据集

javascript - 使用 MongoDB 和 Nodejs 插入和查询日期

javascript - Angular js Intellisense 在 VS 2015 Controller 中不起作用

javascript - 如何将数据从服务器传递到 Service Worker

javascript - React Hook "useContext"在函数 "age"中调用,该函数既不是 React 函数组件,也不是自定义 React Hook 函数

javascript - 我可以在条件渲染中使用多个状态值 - ReactJs

javascript - React Native - 按钮 onPress 不工作

javascript - 如何创建一个具有多种状态的按钮?