React-native-tab-view: Centering tabs + constant tab width (and indicator width)

Created on 25 Dec 2019  ·  7Comments  ·  Source: satya164/react-native-tab-view

Current behaviour

I'm trying to create the following UI for the TabBar, but am having trouble with centering the indicator below the tabs.

Expected result (ignore the icons):
image

Actual result (with the code below):
image

Expected behaviour

The indicator tab should be aligned with the tabs, even if they're centered.

Code sample

<TabBar
  {...props}
  style={{
    backgroundColor: 'white',
  }}
  tabStyle={{
    width: 80,
    marginHorizontal: 8,
  }}
  contentContainerStyle={{
    justifyContent: 'center',
  }}
  labelStyle={{
    fontSize: 13,
    fontWeight: '600',
    textTransform: 'capitalize',
  }}
  indicatorStyle={{
    height: 3,
    bottom: 6,
    backgroundColor: 'rgb(107,132,243)',
  }}
  activeColor={'rgb(107,132,243)'}
  inactiveColor={'rgb(168,170,199)'}
/>

Your Environment

| software | version
| ---------------------------- | -------
| ios or android | either
| react-native | 0.61.4
| react-native-tab-view | 2.11.0
| react-native-gesture-handler | 1.5.2
| react-native-reanimated | 1.4.0
| node | 12.3
| npm or yarn | npm

bug

Most helpful comment

I have an idea.
image
According to the picture, you can set left to center the indicator like this:
indicatorStyle={{ width: w, left: (100 - w) / 2 }}

All 7 comments

Have you solved the problem

To do that you can adjust the margin of the indicator like:

Behaviour

Sample

Code Sample

<TabView
  navigationState={navigationState}
  onIndexChange={index =>
    setNavigationState({ ...navigationState, index })
  }
  renderScene={renderScene}
  initialLayout={{ width: Dimensions.get("window").width }}
  renderTabBar={props => (
    <TabBar
      {...props}
      activeColor={colors.blue}
      inactiveColor={colors.lightText}
      indicatorStyle={{
        backgroundColor: colors.blue,
        marginHorizontal: 40,
        width: 140
      }}
      style={{
        backgroundColor: "white",
        shadowOffset: { height: 0, width: 0 },
        shadowColor: "transparent",
        shadowOpacity: 0,
        elevation: 0,
        marginBottom: 8
      }}
      indicatorContainerStyle={{
        width: Dimensions.get("screen").width
      }}
      contentContainerStyle={{
        justifyContent: "center"
      }}
      tabStyle={{ elevation: 0, width: 140 }}
      labelStyle={{ fontSize: 14 }}
      getLabelText={({ route }) => route.title}
    />
  )}
/>

I have an idea.
image
According to the picture, you can set left to center the indicator like this:
indicatorStyle={{ width: w, left: (100 - w) / 2 }}

Same issue on RTL

Have some issue in here

On my case, i add margin and padding horizontal where value of paddingHorizontal twice from marginHorizontal in indicatorContainerStyle, like this :

indicatorContainerStyle={{ marginHorizontal: 30, paddingHorizontal: 60 }}

Sample :

<TabBar
  {...props}
  activeColor={'rgba(78,55,178,1)'}
  inactiveColor={'rgba(78,55,178,.5)'}
  indicatorStyle={{ backgroundColor: 'rgba(78,55,178,1)' }}
  indicatorContainerStyle={{ marginHorizontal: 40, paddingHorizontal: 80 }}
  labelStyle={[styles.txtBold, { textTransform: 'capitalize' }]}
  style={{ backgroundColor: '#fff', borderBottomColor: '#eee', borderBottomWidth: 1, elevation: 0 }}
/>

It works for me.

tabBar

I have an idea.
image
According to the picture, you can set left to center the indicator like this:
indicatorStyle={{ width: w, left: (100 - w) / 2 }}

This is the solution. Of course 100 is just a placeholder here. You have to replace it by Dimension.get('window').width / Number of Tabs for it work responsively on all screen widths. So it would become
indicatorStyle={{ width: w, left: (Dimensions.get('window').width / Number of tabs - w) / 2 }}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

satya164 picture satya164  ·  3Comments

AndriiUhryn picture AndriiUhryn  ·  3Comments

ios-dev-newbie picture ios-dev-newbie  ·  3Comments

hyochan35 picture hyochan35  ·  3Comments

chen504554911 picture chen504554911  ·  3Comments