React Navigation 導航欄樣式調整+底部角標消息提示

五一佳節匆匆而過,有人選擇在外面看人山人海,有人選擇宅在家中度過五一,也有人依然堅守在第一線,致敬!
這是堅持學習react-native的第二篇文章,可能會遲到,可是毫不會缺席,這篇要涉及到的是react-navigation,也是rn社區主推的一個導航庫。react

網上關於react-navigation的基本使用也是一抓一大把,這裏對於它的使用不作過多介紹,主要記錄使用過程當中的其餘問題。android

由於android 和iOS 手機的不一樣,導航欄的顯示也不太同樣,而這篇文章會盡可能的配置屬性,讓兩端的導航欄樣式、頁面跳轉的動畫保持一致,同時還會介紹底部導航欄添加角標的方法。git

這裏使用的是3.9.1版本,網上好多文章是2.x版本的,用法基本大同小異。github

android 導航欄標題居中適配

默認狀況下,iOS的標題居中顯示,而android的則不!!! react-native

解決createStackNavigatordefaultNavigationOptions屬性裏配置textAlignflexbash

const AppStackNavigator = createStackNavigator({
    HomeScreen: {screen: HomeScreen},
    RainScreen: {screen: RainScreen}
},{
    defaultNavigationOptions:{
        ...
        headerTitleStyle: { 
            ...
            textAlign: "center", //用於android 機型標題居中顯示
            flex:1
        }
    }
})
複製代碼

:android機型標題默認不居中,textAlignflex的屬性配置用於android機型標題居中顯示。
在這種狀況下,若是配置了headerLeft或者headerRight 屬性,會出現標題偏移的現象。
微信


直接在 defaultNavigationOptions裏配置空view的 headerLeftheaderRight

defaultNavigationOptions:{
        ...
        headerTitleStyle: {
            ...
            textAlign: "center", //用於android 機型標題居中顯示
            flex:1,
        },
        headerRight: <View/>,
        headerLeft: <View/>
    }
複製代碼

這時候標題居中,同時能夠在各自的頁面裏面去重寫headerLeft的樣式。學習

android 導航欄去除陰影樣式

android的導航欄還有陰影的樣式,添加elevation 設置陰影的偏移量flex

defaultNavigationOptions:{
    headerStyle:{
        backgroundColor:"#fff",
        elevation:0.5
    },
    ...
}
複製代碼

至此的導航欄的效果跟iOS基本保持一致。

android 頁面跳轉動畫,自右向左打開

默認的android頁面跳轉是自下而上打開頁面,而要與iOS的保持一致的自右向左,配置transitionConfig屬性。動畫

const AppStackNavigator = createStackNavigator({
    HomeScreen: {screen: HomeScreen},
    ...
},{
    defaultNavigationOptions:{
        ...
    },
    transitionConfig: () => ({
        screenInterpolator: (sceneProps) => {
            return StackViewStyleInterpolator.forHorizontal(sceneProps)
        },
    }),
})
複製代碼

底部導航添加消息角標

有時候咱們會遇到這樣的需求,在底部導航處添加消息的角標,提醒用戶閱讀的。


tabBarIcon的屬性裏直接添加圖標顯示的,這裏的 msg變量數值是全局的,只作演示使用,實際項目裏能夠根據接口返回數據,能夠搭配mobx 一塊兒使用。

const rootTab = createBottomTabNavigator({
    ...
    info: {
        screen: InfoStack,
        navigationOptions: {
            tabBarLabel: "消息",
            tabBarIcon: ({focused, tintColor}) => {
                let icon = focused ?
                    require('../resources/img/mine_icon_message_selected.png') :
                    require('../resources/img/mine_icon_message_default.png');
                return <View>
                    {
                        msg > 0 ?
                            <View style={{
                                width:12,
                                height:12,
                                justifyContent:"center",
                                position: 'absolute',
                                zIndex: 9,
                                backgroundColor: "#FB3768",
                                borderRadius:6,
                                right:0,
                                top:-2,
                            }}>
                                <Text style={[{fontSize:10, color:"#fff", textAlign:"center",}]}>{msg}</Text>
                            </View> : null
                    }
                    <Image source={icon} style={{width: 34, height: 26}}/>
                </View>
            }
        }
    }
},{
    ...
    defaultNavigationOptions: ({navigation, screenProps}) => ({
        tabBarOnPress: (obj) => {
            //點擊的時候清除消息
            const {routeName} = obj.navigation.state;
            if (routeName === "info") {
                msg = 0
            }
            obj.navigation.navigate(obj.navigation.state.key)
        }
    })
})
複製代碼

以上幾點是在react-navigation的使用過程當中遇到的問題以及解決方法,相關代碼已經傳到了github上github.com/taixiang/re…,僅供參考,若是有更好的方式 歡迎一塊兒學習研究。

歡迎關注個人我的博客:www.manjiexiang.cn/

更多精彩歡迎關注微信號:春風十里不如認識你
一塊兒學習,一塊兒進步,歡迎上車,有問題隨時聯繫,一塊兒解決!!!

相關文章
相關標籤/搜索