最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react-native:Show spinner at the center of the screen - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 7

Can 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'   
发布评论

评论列表(0)

  1. 暂无评论