Override tab styles using navigation options
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. }
}
+1
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 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.
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! 🙂
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.
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 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 There already is one, could you please review this #2517
@rimzici that's not a pull request, that's a feature request. No code involved there ;)
I see. :)
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)
This feature would be very useful. And here I am I can only wait :(
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.
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!
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.
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.
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} />
);
};
+1
+1
simple: add style in the tab navigator for changing the width or something
<Tab.Navigator
style={{paddingHorizontal: 20}}
>