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

v2 customize tabbar style (I want to use component instead of icon)

Open furkancelik opened this issue 8 years ago • 17 comments

hi all, I am using native navigation v2. I want to customize TabBar's style. I want to use component instead of icon How can I do this? I want to create a tabbar area like the one shown in the picture. image

furkancelik avatar Aug 18 '18 17:08 furkancelik

Would also be interested in this. As far as I can see it, even when trying to just use the icon there is no background styling on

{ 
   options: {
      bottomTab: {
         icon: ...,
         (backgroundColor?)

or am I mistaken?

ghost avatar Aug 22 '18 05:08 ghost

not sure if this is possible at this point in time... but I wrote my own custom component by rendering a row of Touchables at the bottom that calls a function that sets the state of the main container view to render the corresponding tab component

yuannypants avatar Aug 23 '18 01:08 yuannypants

@yuannypants How did you write? Can I see pictures and code?

furkancelik avatar Aug 23 '18 01:08 furkancelik

Was lazy so I just regurgitated some of my code (esp the styling), provides the functionality that you mentioned + tab customisation and badge support as a separate component (TabButton). Screen1 - Screen5 are your tab components, be sure to import them or define them in the same file (not recommended).

export default class MainScreen extends Component {
  constructor(props) {
    super(props);
    this.selectTab = this.selectTab.bind(this);

    this.state = {
      selectedTab: 1
    }

  }

  render() {
    return (
      <View style={mainStyles.main}>
        <View style={mainStyles.tabContainer}>
          {
            this.state.selectedTab===1 ?
              <Screen1 originComponentId={ this.props.componentId }/> :
            this.state.selectedTab===2 ?
              <Screen2 originComponentId={ this.props.componentId }/> :
            this.state.selectedTab===3 ?
              <Screen3 originComponentId={ this.props.componentId }/> :
            this.state.selectedTab===4 &&
              <Screen4 originComponentId={ this.props.componentId }/>
          }
        </View>
        <View style={mainStyles.bottomTabsContainer}>

          <TabButton
            currIndex={this.state.selectedTab}
            index={1}
            tabIconSource={require('./assets/img/tabIcons/tab1Icon.png')}
            tabText={'Tab 1'}
            badgeContent={0}
            selectTab={index => this.selectTab(index)}
          />

          <TabButton
            currIndex={this.state.selectedTab}
            index={2}
            tabIconSource={'require(./assets/img/tabIcons/tab2Icon.png')}
            tabText={'Tab 2'}
            badgeContent={0}
            selectTab={index => this.selectTab(index)}
          />

          <TabButton
            currIndex={this.state.selectedTab}
            index={3}
            tabIconSource={require('./assets/img/tabIcons/tab3Icon.png')}
            tabText={'Tab 3'}
            badgeContent={0}
            selectTab={index => this.selectTab(index)}
          />

          <TabButton
            currIndex={this.state.selectedTab}
            index={4}
            tabIconSource={require('./assets/img/tabIcons/tab4Icon.png')}
            tabText={'Tab 4'}
            badgeContent={0}
            selectTab={index => this.selectTab(index)}
          />

          <TabButton
            currIndex={this.state.selectedTab}
            index={5}
            tabIconSource={require('./assets/img/tabIcons/tab5Icon.png')}
            tabText={'Tab 5'}
            badgeContent={0}
            selectTab={index => this.selectTab(index)}
          />
        </View>
      </View>
    );
  };

  selectTab = (tabId) => {
    this.setState({selectedTab: tabId});
    switch (tabId) {
      case 1: // Tab 1
        // any other action you require other than setState,
        // e.g. mergeOptions to change topbar content?
        break;

      case 2: // Tab2
        break;

      case 3: // Tab3
        break;

      case 4: // Tab4
        break;
    }
  }

class TabButton extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <TouchableHighlight style={{flex:1, alignItems: 'center', justifyContent: 'center'}} underlayColor={'transparent'} onPress={() => this.props.selectTab(this.props.index)}>
        <View style={mainStyles.bottomTabContainer}>
          <Image
            source={this.props.tabIconSource}
            style={
              this.props.index!==0 ?
                this.props.currIndex === this.props.index ?
                  mainStyles.selectedTabIcon :
                  mainStyles.tabIcon :
                this.props.currIndex === this.props.index ?
                  mainStyles.selectedListTabIcon :
                  mainStyles.listTabIcon
          />
          <Text style={this.props.currIndex === this.props.index ? mainStyles.selectedTabTitle : mainStyles.tabTitle}>{this.props.tabText}</Text>
          {
            this.props.badgeContent!==0 &&
            <View style={this.props.badgeContent<10 ? mainStyles.badgeContainer : mainStyles.extendedBadgeContainer}>
              <Text style={mainStyles.badge}>
                {this.props.badgeContent<99 ? this.props.badgeContent : '99+'}
              </Text>
            </View>
          }
        </View>
      </TouchableHighlight>
    )
  }
}


const mainStyles = StyleSheet.create({
  main: {
    flex: 1
  },
    tabContainer: {
      position: 'absolute',
      top:0,
      left:0,
      height: deviceHeight-49-55,
      width: deviceWidth,
    },
    bottomTabsContainer: {
      flexDirection: 'row',
      alignItems: 'center',
      justifyContent: 'space-between',
      position: 'absolute',
      left:0,
      bottom:0,
      height: 50,
      width: deviceWidth,
      backgroundColor: 'white',
      borderTopWidth: 0.5,
      borderTopColor: '#b6b6b6'
    },
      bottomTabContainer: {
        height: 50,
        width: 50,
        alignItems: 'center',
        justifyContent: 'flex-end'
      },
        tabIcon: {
          height: 23,
          width: 23,
          resizeMode: 'contain',
          tintColor: '#767676',
        },
        selectedTabIcon: {
          height: 24,
          width: 24,
          resizeMode: 'contain',
          tintColor: '#6DCDD7',
        },
        listTabIcon: {
          height: 48,
          width: 48,
          resizeMode: 'contain',
          tintColor: '#767676',
        },
        selectedListTabIcon: {
          height: 48,
          width: 48,
          resizeMode: 'contain',
          tintColor: '#6DCDD7',
        },
        tabTitle: {
          fontFamily: 'Nunito-Bold',
          fontSize: 13,
          color: '#767676',
          marginBottom: 3
        },
        selectedTabTitle: {
          fontFamily: 'Nunito-Bold',
          fontSize: 14,
          color: '#6DCDD7',
          marginBottom: 3
        },
          badgeContainer: {
            position: 'absolute',
            alignItems: 'center',
            justifyContent: 'center',
            top: 2,
            right: 6,
            backgroundColor: '#FF8484',
            height: 16,
            width: 16,
            borderRadius: 8,
          },
            extendedBadgeContainer: {
              position: 'absolute',
              alignItems: 'center',
              justifyContent: 'center',
              top: -2,
              right: -6,
              backgroundColor: '#FF8484',
              height: 16,
              width: 22,
              borderRadius: 8,
            },
            badge: {
              fontFamily: 'Nunito-Bold',
              fontSize: 12,
              color: 'white'
            }
});

const mainStyles = StyleSheet.create({
  main: {
    flex: 1
  },
    tabContainer: {
      position: 'absolute',
      top:0,
      left:0,
      height: deviceHeight-49-55,
      width: deviceWidth,
    },
    bottomTabsContainer: {
      flexDirection: 'row',
      alignItems: 'center',
      justifyContent: 'space-between',
      position: 'absolute',
      left:0,
      bottom:0,
      height: 50,
      width: deviceWidth,
      backgroundColor: 'white',
      borderTopWidth: 0.5,
      borderTopColor: '#b6b6b6'
    },
      bottomTabContainer: {
        height: 50,
        width: 50,
        alignItems: 'center',
        justifyContent: 'flex-end'
      },
        tabIcon: {
          height: 23,
          width: 23,
          resizeMode: 'contain',
          tintColor: '#767676',
        },
        selectedTabIcon: {
          height: 24,
          width: 24,
          resizeMode: 'contain',
          tintColor: '#6DCDD7',
        },
        listTabIcon: {
          height: 48,
          width: 48,
          resizeMode: 'contain',
          tintColor: '#767676',
        },
        selectedListTabIcon: {
          height: 48,
          width: 48,
          resizeMode: 'contain',
          tintColor: '#6DCDD7',
        },
        tabTitle: {
          fontFamily: 'Nunito-Bold',
          fontSize: 13,
          color: '#767676',
          marginBottom: 3
        },
        selectedTabTitle: {
          fontFamily: 'Nunito-Bold',
          fontSize: 14,
          color: '#6DCDD7',
          marginBottom: 3
        },
          badgeContainer: {
            position: 'absolute',
            alignItems: 'center',
            justifyContent: 'center',
            top: 2,
            right: 6,
            backgroundColor: '#FF8484',
            height: 16,
            width: 16,
            borderRadius: 8,
          },
            extendedBadgeContainer: {
              position: 'absolute',
              alignItems: 'center',
              justifyContent: 'center',
              top: -2,
              right: -6,
              backgroundColor: '#FF8484',
              height: 16,
              width: 22,
              borderRadius: 8,
            },
            badge: {
              fontFamily: 'Nunito-Bold',
              fontSize: 12,
              color: 'white'
            }
});

yuannypants avatar Aug 24 '18 11:08 yuannypants

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest version and report back. Thank you for your contributions.

stale[bot] avatar Oct 08 '18 11:10 stale[bot]

i am interested in too

befirst avatar Oct 09 '18 19:10 befirst

I'm also searching for a way of doing this that isn't too hacky.

Brianwebb22 avatar Oct 29 '18 18:10 Brianwebb22

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.

stale[bot] avatar Nov 28 '18 19:11 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Dec 05 '18 20:12 stale[bot]

+1

ghost avatar Jan 08 '19 11:01 ghost

Has anyone found anything not too hacky to achieve this?

scaralfred avatar Feb 25 '19 10:02 scaralfred

I need too something like that.

rcerrejon avatar Apr 15 '19 10:04 rcerrejon

+1

AyushAppin avatar Jul 30 '19 07:07 AyushAppin

I'm looking to do the same thing, but in the navbar. I don't know how to access the onPress event with a custom component... Any ideas ? Thanks ;)

Ghya avatar Jul 31 '19 13:07 Ghya

+1

devilabhi avatar Aug 10 '19 05:08 devilabhi

I found a tutorial on youtube. Hope this help https://www.youtube.com/watch?v=qwkeM0W2dfo

nustalitha avatar Nov 27 '19 08:11 nustalitha

I'm looking to do the same thing, but in the navbar. I don't know how to access the onPress event with a custom component... Any ideas ? Thanks ;)

Same problem

DaltonHarvey avatar Jun 14 '20 22:06 DaltonHarvey