react-navigation / react-navigation.github.io

Home of the documentation and other miscellanea
https://reactnavigation.org/
MIT License
312 stars 1.89k forks source link

Swipeable TabScreen #737

Open Uttu316 opened 4 years ago

Uttu316 commented 4 years ago

How can I enable swipe in TabScreen? I want to change screens when the user swipes on it in (Android and IOS). Is there any property that I am missing?

<Tab.Navigator
      initialRouteName="home"
      tabBarOptions={{
        keyboardHidesTabBar: true,
      }}
      tabBar={props => <CustomBottomBar {...props} />}>
      <Tab.Screen
        name="home"
        component={HomeScreen}
        options={{
          tabBarLabel: 'Abc',
        }}
      />
      <Tab.Screen
        name="splash"
        component={SplashScreen}
        options={{
          tabBarLabel: 'Xyz',
        }}
      />
    </Tab.Navigator>

function CustomTabBar({ state, descriptors, navigation }) {
  return (
    <View style={{ flexDirection: 'row' }}>
      {state.routes.map((route, index) => {
        const { options } = descriptors[route.key];
        const label =
          options.tabBarLabel !== undefined
            ? options.tabBarLabel
            : options.title !== undefined
            ? options.title
            : route.name;

        const isFocused = state.index === index;

        const onPress = () => {
          const event = navigation.emit({
            type: 'tabPress',
            target: route.key,
            canPreventDefault: true,
          });

          if (!isFocused && !event.defaultPrevented) {
            navigation.navigate(route.name);
          }
        };

        const onLongPress = () => {
          navigation.emit({
            type: 'tabLongPress',
            target: route.key,
          });
        };

        return (
          <TouchableOpacity
            accessibilityRole="button"
            accessibilityStates={isFocused ? ['selected'] : []}
            accessibilityLabel={options.tabBarAccessibilityLabel}
            testID={options.tabBarTestID}
            onPress={onPress}
            onLongPress={onLongPress}
            style={{ flex: 1 }}
          >
            <Text style={{ color: isFocused ? '#673ab7' : '#222' }}>
              {label}
            </Text>
          </TouchableOpacity>
        );
      })}
    </View>
  );
}
jarvisluong commented 4 years ago

There are 3 kinds of tab navigator which react-navigation provide, and the one which support swiping is material top tab (https://reactnavigation.org/docs/material-top-tab-navigator). You need to use this kind of tab navigator to achieve the swiping feature.