Search code examples
javascriptreactjsreact-nativenavigationnavigator

React Navigation switching background colors and styling StackNavigator


I'm fairly new to React Native, but I have a simple working app with three scenes. I was previously using Navigator but it felt laggy and was excited to try out React Navigation (as in https://reactnavigation.org/). After implementing React Navigation, my background color switched from white to grey, and what was grey to white. This is a strange and shouldn't be related. However I didn't change my styles. I only implemented the new navigation and the colors changed. When I revert back to Navigator my colors return. I'm using StackNavigator. Has anyone else encountered this strange phenomenon?

Or maybe a better question is : how do I style my header and background color in React Navigation's StackNavigator?


Solution

  • To style the header in React Navigation use a header object inside the navigationOptions object:

    static navigationOptions = {  
      header: {
        titleStyle: {
         /* this only styles the title/text (font, color etc.)  */
        },
        style: {
         /* this will style the header, but does NOT change the text */
        },
        tintColor: {
          /* this will color your back and forward arrows or left and right icons */
        }
      }
    }
    

    For styling the backgroundColor, you just need to set the backgroundColor in your app otherwise you'll get the default color.

    UPDATE!! As of May 2017 beta9 the navigationOptions are now flat

    You can read about the breaking change here

    You need to remove the object keys from the header object. Also, notice they have been renamed.

    static navigationOptions = {
       title: 'some string title',
       headerTitleStyle: {
          /*  */
       },
       headerStyle: {
          /*  */
       },
       headerTintColor: {
          /*  */
       },
    }