Reputation: 217
I have started playing with React Native and I'm trying to create a simple image button component just to get used to the language. Here is what I have:
'use strict';
const React = require('React');
const PropTypes = require('prop-types');
import {
Platform, TouchableOpacity,
StyleSheet, Dimensions,
Text, Image,
View
} from 'react-native';
const TouchableNativeFeedback = require('TouchableNativeFeedback');
const MyBrown = '#89664C';
const styles = StyleSheet.create({
button: Platform.select({
ios: {
},
android: {
elevation: 4,
backgroundColor: MyBrown,
borderRadius: 2
}
}),
text: Platform.select({
ios: {
color: 'white',
textAlign: 'center',
padding: 8,
fontSize: 18
},
android: {
color: 'white',
textAlign: 'center',
padding: 8,
fontWeight: '500',
}
}),
icon: {
width: 60,
height: 60,
}
});
export default class MyButton extends React.Component{
render(){
return(
<View style={styles.button}>
<TouchableOpacity>
<Image source={this.props.src} style={styles.icon} />
<Text style={styles.text}>{this.props.text}</Text>
</TouchableOpacity>
</View>
)
}
}
This renders as I'd expect when I run the app, but the image is left aligned whilst the text is centered. Now I thought I could use Flexbox and add {flex:1} to the parent View or TouchOpacity elements, but as soon as I do this the control no longer renders in my app.
Two questions on this:
Thank you
Upvotes: 6
Views: 14916
Reputation: 599
Basically the best way to center image is to wrap it with parent view and add flex styles to it and there is no need to pass flex: 1
> justifyContent: 'center',
> alignItems: 'center',
Upvotes: 7