2017-03-17 39 views
7

我在我的React Native應用程序中實現React Navigation,並且我想要更改標題的背景和前景顏色。我有以下幾種:React Native,更改React導航標題樣式

/** 
* Sample React Native App 
* https://github.com/facebook/react-native 
* @flow 
*/ 

import React, { Component } from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View 
} from 'react-native'; 
import { StackNavigator } from 'react-navigation'; 

export default class ReactNativePlayground extends Component { 

    static navigationOptions = { 
    title: 'Welcome', 
    }; 

    render() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}> 
      Welcome to React Native! 
     </Text> 
     <Text style={styles.instructions}> 
      To get started, edit index.android.js 
     </Text> 
     <Text style={styles.instructions}> 
      Double tap R on your keyboard to reload,{'\n'} 
      Shake or press menu button for dev menu 
     </Text> 
     </View> 
    ); 
    } 

} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    }, 
    instructions: { 
    textAlign: 'center', 
    color: '#333333', 
    marginBottom: 5, 
    }, 
}); 

const SimpleApp = StackNavigator({ 
    Home: { screen: ReactNativePlayground } 
}); 

AppRegistry.registerComponent('ReactNativePlayground',() => SimpleApp); 

默認情況下,標題的背景顏色是白色,前景爲黑色。我也查看了React Navigation的文檔,但是我無法找到它在哪裏顯示如何設置樣式。任何幫助?

回答

15

在新版本陣營導航你有一個平坦的設置對象,象下面這樣:

static navigationOptions = { 
    title: 'Chat', 
    headerStyle: { backgroundColor: 'red' }, 
    headerTitleStyle: { color: 'green' }, 
} 

過時的答案:

%的文檔,here,修改navigationOptions對象。嘗試像這樣:

static navigationOptions = { 
    title: 'Welcome', 
    header: { 
     style: {{ backgroundColor: 'red' }}, 
     titleStyle: {{ color: 'green' }}, 
    } 
} 

請不要實際上最終使用這些顏色!

+1

衛生署!我正在查看文檔的錯誤部分。謝謝 – Tiwaz89

+1

沒問題,我昨天剛剛這樣做的事實是我能夠提供幫助的唯一原因。我不得不沖刷文檔來找到它。我很高興這並非一帆風順。如果您的ReactNavigation問題太多,請查看4。ReactRouter的0分支,它有本地綁定。 – cssko

+0

真棒我想我會這樣做,再次感謝! :) – Tiwaz89

2

試試這個代碼:

static navigationOptions = { 
    headerTitle: 'SignIn', 
    headerTintColor: '#F44336' 
    }; 

好運!

+0

這不是設計頭的樣式,這是爲Title設置顏色,downvote –

1

注意! navigationOptionsStack NavigationDrawer Navigation之間的差異
堆棧導航已解決。
但對於抽屜導航您可以用contentComponent配置添加自己的頭,讓你的風格:
首先import { DrawerItems, DrawerNavigation } from 'react-navigation'然後

頭之前DrawerItems

contentComponent: props => <ScrollView><Text>Your Own Header Area Before</Text><DrawerItems {...props} /></ScrollView>

Header Before DrawerItems

頁腳DrawerItems後:

contentComponent: props => <ScrollView><DrawerItems {...props} /><Text>Your Own Footer Area After</Text></ScrollView>

0

試試這個工作代碼

static navigationOptions = { 
     title: 'Home', 
     headerTintColor: '#ffffff', 
     headerStyle: { 
     backgroundColor: '#2F95D6', 
     borderBottomColor: '#ffffff', 
     borderBottomWidth: 3, 
     }, 
     headerTitleStyle: { 
     fontSize: 18, 
     }, 
    };