I want to show the spinner at the center of screen.I used alignItems, justifyContent and flex but there is no change in the position then i use margintop for correct positioning.why there is no change on the use of alignItems,justifyContent and flex?
here i m using spinner.
return (
<View style={style.spinnerStyle}>
<Spinner size ="large" />
</View>
);
const style = {
spinnerStyle: {
flex: 1,
marginTop:240
}
};
Spinner.js
import React from 'react';
import {View,ActivityIndicator} from 'react-native';
const Spinner = ({size}) => {
return (
<View style = {styles.spinnerStyle}>
<ActivityIndicator size={size || 'large'} />
</View>
);
};
const styles = {
spinnerStyle: {
flex: 1,
justifyContent: 'center',
alignItems:'center'
}
};
export { Spinner };
I want to show the spinner at the center of screen.I used alignItems, justifyContent and flex but there is no change in the position then i use margintop for correct positioning.why there is no change on the use of alignItems,justifyContent and flex?
here i m using spinner.
return (
<View style={style.spinnerStyle}>
<Spinner size ="large" />
</View>
);
const style = {
spinnerStyle: {
flex: 1,
marginTop:240
}
};
Spinner.js
import React from 'react';
import {View,ActivityIndicator} from 'react-native';
const Spinner = ({size}) => {
return (
<View style = {styles.spinnerStyle}>
<ActivityIndicator size={size || 'large'} />
</View>
);
};
const styles = {
spinnerStyle: {
flex: 1,
justifyContent: 'center',
alignItems:'center'
}
};
export { Spinner };
Share
Improve this question
asked Jul 26, 2017 at 7:04
Chaitanya ParasharChaitanya Parashar
7423 gold badges12 silver badges21 bronze badges
4 Answers
Reset to default 7Can you please try with following:
where you are using Spinner
spinnerStyle: {
flex: 1,
marginTop:240,
justifyContent: 'center',
alignItems:'center'
}
In Spinner
spinnerStyle: {
flex: 1
alignSelf:'center'
}
const styles = StyleSheet.create({
spinnerStyle: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'center'
}
});
The correct way to vertically center the ActivityIndicator ponent is to use 'flex: 1' on the ActivityIndicator ponent itself, not on its parent ponent. It does not need a wrapper ponent and none of the other flex alignment styles are necessary at all.
The improved implementation of the OP's code would be:
import React from 'react';
import {ActivityIndicator} from 'react-native';
const Spinner = ({size}) => {
return (
<ActivityIndicator size={size || 'large'} style={{ flex: 1 }}/>
);
};
export { Spinner };
Set in spinner parent view:
justifyContent: 'center',
alignItems:'center'
And in spinner set:
alignSelf:'center'