Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change to color of react-native-tab-view?

I am new to react-native and learning it. While learning react-native-tab-view, I tried to change it's color but after several tries I was unable to do it. I shall be really grateful if someone guide me how to change to color of the tab-bar which is blue by-default. Here is the link to npm react-native-tab-view and here is the piece of code. Any help would be highly appreciated.

import * as React from 'react';
import { View, StyleSheet, Dimensions } from 'react-native';
import { TabView, SceneMap } from 'react-native-tab-view';
 
const FirstRoute = () => (
  <View style={[styles.scene, { backgroundColor: '#ff4081' }]} />
);
 
const SecondRoute = () => (
  <View style={[styles.scene, { backgroundColor: '#673ab7' }]} />
);
 
const initialLayout = { width: Dimensions.get('window').width };
 
export default function TabViewExample() {
  const [index, setIndex] = React.useState(0);
  const [routes] = React.useState([
    { key: 'first', title: 'First' },
    { key: 'second', title: 'Second' },
  ]);
 
  const renderScene = SceneMap({
    first: FirstRoute,
    second: SecondRoute,
  });
 
  return (
    <TabView
      navigationState={{ index, routes }}
      renderScene={renderScene}
      onIndexChange={setIndex}
      initialLayout={initialLayout}
    />
  );
}
 
const styles = StyleSheet.create({
  scene: {
    flex: 1,
  },
});
like image 486
Dilawer Hussain Avatar asked Oct 26 '20 10:10

Dilawer Hussain


People also ask

How do I customize tab navigation in react native?

Add icons to the tab bar To add icons to each tab, first import the Icon component from react-native-vector-icons library inside the navigation/TabNavigator/index. js file. For this example, let's use AntDesign based icons. // after other import statements import Icon from 'react-native-vector-icons/AntDesign';


2 Answers

Changing the background color of the tab-bar

If you refer to this section, the styling props for the tab-bar will have to be passed after declaring a custom React component using the renderTabBar prop.

<TabView
  navigationState={{ index, routes }}
  renderScene={renderScene}
  onIndexChange={setIndex}
  initialLayout={initialLayout}
  renderTabBar={props => <TabBar {...props} style={{backgroundColor: 'black'}}/>} // <-- add this line
/>

Changing the text color of the tab-bar

If you refer to this,

<TabBar
  renderLabel={({route, color}) => (
    <Text style={{ color: 'black', margin: 8 }}>
      {route.title}
    </Text>
  )}
  style={{backgroundColor: 'white'}}
  ...
/>

Feel free to experiment further using the example snack. :)

like image 94
kenmistry Avatar answered Oct 22 '22 09:10

kenmistry


enter image description here

Here is my implementation of how I achieved it

customize the TabBar then use it in the TabView like below code

first thing first let's initialize the states first

   const [index, setIndex] = useState(0);
   const [routes] = useState([
     { key: 'first', title: 'Main Tab' },
     { key: 'second', title: 'Second Tab' }]);

   const renderScene = SceneMap({
     first: FeaturedStore,
     second: AllStore });
    
  

to change the style of the labels/titles this might help you. here I have used my custom TextView you can use your own

 const renderTabBar = props => {
    return (
      <TabBar
        {...props}
        renderLabel={({ focused, route }) => {
          return (
            <TextView
              size={20}
              category="Medium"
              color={focused ? 'BLACK' : 'GRAY3'}>
              {route.title}
            </TextView>
          );
        }}
        indicatorStyle={styles.indicatorStyle}
        style={styles.tabBar}
      />
    );
  };

this is the sample code for TabView

 <View style={styles.container}>
    <View style={styles.divider} />
    <TabView
      renderTabBar={renderTabBar}
      navigationState={{ index, routes }}
      renderScene={renderScene}
      onIndexChange={setIndex}
      initialLayout={{ width: SCREEN_WIDTH }}
      showPageIndicator={true}
    />
  </View>

Let's put the cherry on the cake below is the styling making this TabView looks like this

 const styles = StyleSheet.create({
  container: { width: '100%', height: '100%', backgroundColor: COLORS.WHITE },
  tabBar: {
    backgroundColor: '#ffffff',
    borderBottomWidth: 1,
    borderColor: COLORS.BLACK,
  },
  indicatorStyle: {
    backgroundColor: COLORS.PRIMARY,
    padding: 1.5,
    marginBottom: -2,
  },
  divider: {
    zIndex: 100,
    position: 'absolute',
    width: 1,
    height: 48,
    backgroundColor: 'black',
    alignSelf: 'center',
  },
});

for further information, you can visit official docs

like image 25
Harvinder Singh Avatar answered Oct 22 '22 09:10

Harvinder Singh