How to Center an Icon in a Button?

285 views Asked by At

I am trying to use a button for navigation, so I want the button to be a group of an icon and text that I can click to navigate to a new page. The functionality works by doing this:

<FontAwesome5.Button style={styles.iconButton} name="history" size={10} color={Colors.light.tabBackground} onPress={() => navigation.navigate('TabOneScreen')}>
    <Text style={styles.subText} lightColor={Colors.light.tabBackground}>History</Text>
</FontAwesome5.Button>

...

const styles = StyleSheet.create({
    iconButton: {
        flexDirection: "column",
        justifyContent: 'center',
        alignItems: 'center',
        width: 100,
        backgroundColor: 'red',
      },
  });

This button is rendered like so:

enter image description here

The vertical spacing is fine. However, while the text is centered horizontally, the icon obviously isn't. No matter what styles I have tried, I have been unable to get the icon and text to both be centered horizontally as a button. The background would normally be removed, but I added it here for debug purposes.

1

There are 1 answers

1
Nader Alfakesh On

add the style directly to the icon :

<FontAwesome5.Button iconStyle={{ alignSelf: 'center' }}  ...ther props>
...text
</FontAwesome5.Button>