react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

Override tab styles using navigation options

Open jackread opened this issue 9 years ago • 21 comments

I'd like to be able to set custom styles on individual tabs. e.g. each could have a different backgroundColor or have a custom width set.

The navigation options looks like a good place to accomplish this. Something like this...

static navigationOptions = {
    tabBarIcon: ({ tintColor }) => (
      
    ),
    tabStyle: { backgroundColor: 'red', marginRight: 20, width: 100... etc. }
}

jackread avatar May 11 '17 18:05 jackread

+1

ghost avatar May 17 '17 13:05 ghost

This issue can be closed now as it has been resolved with PR #1667. Please check this issue #1334 for even more options on styling tabs.

gabrielbull avatar May 25 '17 13:05 gabrielbull

@gabrielbull not sure this does what I was hoping...

the tabBarOptions are set on the toolbar globally. I'm hoping to be able to set style for tabs in the Screen Navigator options (https://reactnavigation.org/docs/navigators/tab#Screen-Navigation-Options)... basically letting you set styles for tabs on an individual basis.

jackread avatar May 25 '17 14:05 jackread

Ah, got it, you're right, this sets them for all tabs, which is also useful. Okay, let's do it so we can set them individually too! 🙂

gabrielbull avatar May 25 '17 14:05 gabrielbull

I've hacked together a work around...

If we pass in a function to tabBarOptions that receives the scene as an argument it's relatively easy to add new styles.

renderTabStyle: (scene)=> {
  if (scene.index == 1) {
    return { marginRight: 35 }
  }
  else if (scene.index == 2) {
    return { marginLeft: 35 }
  }
  else {
    return {}
  }
}

In TabBarTop.js

type Props = {
  ...
  renderTabStyle: (scene: TabScene) => Style,
}

...

_renderTabStyle = (scene: TabScene) => {
  const {
    renderTabStyle
  } = this.props;
  if (!renderTabStyle) {
    return null;
  }
  return renderTabStyle(scene);
};

render() {
  const {
    ...
    renderTabStyle
  } = this.props

 ...

  return (
    
    Animated.View
      style={[
        styles.tab,
        { backgroundColor, justifyContent },
        this._renderTabStyle(scene)
      ]}

   ...

  )
 
}

Any objections to this approach? I guess with a few tweaks we could set the functions in navigation options like renderIcon and renderLabel.

jackread avatar May 25 '17 16:05 jackread

Any update on this feature? It will be a lot more flexible to have an option to override the tabStyle, and to have a control at individual tabs.

rimzici avatar Sep 06 '17 05:09 rimzici

@rimzici that I know of no open PRs tackle this FR, feel free to open a new one and I'll do my best to review it asap.

kelset avatar Oct 14 '17 07:10 kelset

@kelset There already is one, could you please review this #2517

rimzici avatar Oct 15 '17 13:10 rimzici

@rimzici that's not a pull request, that's a feature request. No code involved there ;)

kelset avatar Oct 15 '17 17:10 kelset

I see. :)

rimzici avatar Oct 26 '17 11:10 rimzici

I'll keep this open as FR - but in the meantime you can use this: https://github.com/react-community/react-navigation/issues/2818

(closed by mistake, wrong button)

kelset avatar Oct 28 '17 13:10 kelset

This feature would be very useful. And here I am I can only wait :(

arfianadam avatar Nov 02 '17 10:11 arfianadam

here I am I can only wait :(

That's not true: this is an open source project, you have free access to the whole codebase composing this library. This means that you can copy and modify everything, and even contribute back to the project by sharing your tweaks and features and modifications via Pull Requests.

We, maintainers, are working hard to make the lib a good solution to use for react-native, but we are a group of people mostly dedicating free time to it; we have to prioritize tasks, and this is not a priority at the moment.

But, again, that's why this is open source: so that anyone can take upon themselves to add this feature and submit a PR.

kelset avatar Nov 02 '17 10:11 kelset

please re-post to https://react-navigation.canny.io/feature-requests if this is important for you. re-organizing issues to focus on bug reports here and feature requests on canny. thanks!

brentvatne avatar Jan 23 '18 00:01 brentvatne

I customized tabBarComponent in order to change position and tintColor every page.
See this gist : https://gist.github.com/watanabeyu/c7e9546e86527faecb12e5407436e465

if in HomeScreen(navigationState.index = 0), override style and activeTineColor and inactiveTintColor.

try it.

watanabeyu avatar Feb 09 '18 06:02 watanabeyu

Hey @jackread, does the tabBarButtonComponent sound like a good fit for your case? Guess u can use a TouchableOpacity with custom styles for each one of your tab options:

import {TouchableOpacity} from 'react-native';

const customStyles = {
  backgroundColor: 'black'
};

static navigationOptions = {
    tabBarIcon: ...
    tabBarButtonComponent: props => <TouchableOpacity {...props} style={customStyles} />
}

Hope it helps somehow.

igorrfc avatar Sep 13 '18 23:09 igorrfc

Thanks @watanabeyu.

For the current version of react-navigation (2.14), we need to use BottomTabBar instead of TabBarBottom:

import { createBottomTabNavigator } from 'react-navigation';
import { BottomTabBar } from 'react-navigation-tabs';

const CustomBottomTabBar = (props) => {
  let newProps = props;
  if (props.navigation.state.index === 0) {
    newProps = {
      ...props,
      activeTintColor: '#fff',
      inactiveTintColor: '#fff',
      style: {
        ...props.style,
        backgroundColor: 'transparent',
      },
      indicatorStyle: { backgroundColor: 'transparent' },
    };
  }

  return (
      <BottomTabBar {...newProps} />
  );
};

ldgarcia avatar Sep 16 '18 19:09 ldgarcia

+1

brunohkbx avatar May 08 '19 00:05 brunohkbx

+1

TomasSestak avatar Oct 22 '20 12:10 TomasSestak

simple: add style in the tab navigator for changing the width or something

<Tab.Navigator
         style={{paddingHorizontal: 20}}
 >

khokanuzzaman avatar Mar 02 '22 09:03 khokanuzzaman