【问题标题】:React Native: How to set background color for the whole app and individual scene in react-native-router-flux?React Native:如何在 react-native-router-flux 中为整个应用程序和单个场景设置背景颜色?
【发布时间】:2016-09-21 02:12:47
【问题描述】:

在使用 react-native-router-flux 的 React Native 中,如何设置整个应用程序以及单个场景的背景颜色?

这是我目前的设置:

const RouterWithRedux = connect()(Router)
const store = configureStore()

export default class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <RouterWithRedux>
          <Scene key='root'>
            <Scene initial={true} key='login' component={Login} title='Login Page'/>
            <Scene key='register' component={Register} title='Register'/>
          </Scene>
        </RouterWithRedux>
      </Provider>
    )
  }
}

谢谢

【问题讨论】:

标签: javascript reactjs react-native react-native-router-flux


【解决方案1】:

API documentation 中,您可以在RouterScene 上使用sceneStylegetSceneStyle(尽管Router 只能使用getSceneStyle)。

编辑

Router 中只能使用getSceneStyle 并且必须传递一个函数

// ..
const getSceneStyle = (/* NavigationSceneRendererProps */ props, computedProps) => {
  const style = {
    backgroundColor: 'blue',
  };
  return style;
};
// ..
<Router getSceneStyle={getSceneStyle} {...otherProps}>
// ..

对于您的Scenes,您可以将一个对象或StyleSheet 对象传递给sceneStyle 或使用getSceneStyle(确保您传递一个类似上述的函数):

<Scene
  key="my-scene"
  component={MyScene}
  sceneStyle={{
    backgroundColor: 'red',
  }}
  title="My Scene"  />

这将覆盖蓝色背景。

【讨论】:

  • 抱歉,您能说明一下它如何应用于我拥有的代码吗?我试过了,但没有用。
  • const RouterWithRedux = connect()(&lt;Router styleStyle={{backgroundColor: 'black'}}/&gt;)
  • 嗨,您有机会看到我之前的评论吗?
  • 路由器没有工作,所以每个场景都做了&lt;Scene component={Login} initial={true} key='login' sceneStyle={{backgroundColor: 'black'}} title='Login'/&gt; 并且工作了。但是想知道您是否不允许在&lt;Scene key='root'/&gt; 上设置sceneStyle?因为它不起作用。似乎为每个场景做过度工作。此外,即使我将 backgroundColor 设置为黑色,在过渡期间似乎还有另一个背景是白色的。你怎么能改变它的颜色呢?谢谢rclai,一旦回答就会采取答案。
  • 你可以看看我更新的答案。我运行了react-native-router-flux 拥有的示例项目,并且能够验证它是否有效。另外,我不知道您是否注意到,但在您之前的评论中,您说您是如何尝试在您的RouterWithRedux 上设置样式的,但是您打错字并写了styleStyle,而您的意思是sceneStyle,尽管如此,即使你纠正了你的错字,你仍然必须使用getSceneStyle 并传递一个函数。
猜你喜欢
  • 1970-01-01
  • 2017-08-19
  • 1970-01-01
  • 2017-04-20
  • 2017-01-30
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多