取决于屏幕,本机材质的顶部选项卡导航器滑动禁用

时间:2020-08-27 07:45:17

标签: react-native react-navigation

仅想禁用地图组件的滑动功能,而是在使用“ swipeEnabled”时应用了整个屏幕。

我该怎么办?

const Tab = createMaterialTopTabNavigator();

const Tabs = () => {

  return (
    <Tab.Navigator
      swipeEnabled={false}  // <- Screens can be swiped but it is applied to every screen.
      {...}
    >
      <Tab.Screen
        name="Home"
        component={Home}
      />
      <Tab.Screen
        name="Map"
        component={Map}
      /> 
    </Tab.Navigator>
  );
}

const App = () => {
  return (
    <NavigationContainer>
      <SafeAreaView style={styles.safeAreaView} />
      <Tabs />
    </NavigationContainer>
  );
}

1 个答案:

答案 0 :(得分:1)

如果您处于swipeEnabled屏幕上,则可以将状态值传递给false并将其更新为Map

const Tab = createMaterialTopTabNavigator();

const Tabs = () => {
  const [swipeEnabled, setSwipeEnabled] = useState(true);
  return (
    <NavigationContainer>
      <Tab.Navigator
        swipeEnabled={swipeEnabled}
        screenOptions={({ navigation, route }) => {
          if (route.name === 'Map' && navigation.isFocused()) {
            setSwipeEnabled(false);
          } else if (route.name !== 'Map' && navigation.isFocused()) {
            setSwipeEnabled(true);
          }
        }}>
        <Tab.Screen name="Home" component={Home} />
        <Tab.Screen name="Map" component={Map} />
      </Tab.Navigator>
    </NavigationContainer>
  );
};