『React Navigation 3x系列教程』createMaterialTopTabNavigator開發指南

這篇文章將向你們分享createMaterialTopTabNavigator的一些開發指南和實用技巧。html

createMaterialTopTabNavigator.png

createMaterialTopTabNavigator API

createMaterialTopTabNavigator(RouteConfigs, TabNavigatorConfig):react

  • RouteConfigs(必選):路由配置對象是從路由名稱到路由配置的映射,告訴導航器該路由呈現什麼。
  • TabNavigatorConfig(可選):配置導航器的路由(如:默認首屏,navigationOptions,paths等)樣式(如,轉場模式mode、頭部模式等)。

從createMaterialTopTabNavigator API上能夠看出createMaterialTopTabNavigator支持經過RouteConfigsTabNavigatorConfig兩個參數來建立createMaterialTopTabNavigator導航器。ios

RouteConfigs

RouteConfigs支持三個參數screenpath以及navigationOptionsreact-native

  • screen(必選):指定一個 React 組件做爲屏幕的主要顯示內容,當這個組件被TabNavigator加載時,它會被分配一個navigation prop。
  • path(可選):用來設置支持schema跳轉時使用,具體使用會在下文的有關Schema章節中講到;
  • navigationOptions(可選):用以配置全局的屏幕導航選項如:title、headerRight、headerLeft等;

TabNavigatorConfig

  • tabBarComponent:指定TabNavigator的TabBar組件;
  • tabBarPosition: 用於指定TabBar的顯示位置,支持'top' 與 'bottom'兩種方式;
  • swipeEnabled : 是否能夠左右滑動切換tab;
  • lazy - 默認值是 false。若是是true,Tab 頁只會在被選中或滑動到該頁時被渲染。當爲 false 時,全部的 Tab 頁都將直接被渲染;(能夠輕鬆實現多Tab 頁面的懶加載);
  • optimizationsEnabled -是否將 Tab 頁嵌套在到 中。若是是,一旦該 Tab 頁失去焦點,將被移出當前頁面, 從而提升內存使用率。
  • animationEnabled : 切換頁面時是否有動畫效果。
  • initialLayout : 包含初始高度和寬度的可選對象能夠被傳遞以防止react-native-tab-view呈現中的一個幀延遲;
  • tabBarOptions: 配置TaBar下文會詳細講解;
  • initialRouteName : 默認頁面組件,TabNavigator顯示的第一個頁面;
  • order: 定義tab順序的routeNames數組。
  • paths: 提供routeName到path config的映射,它覆蓋routeConfigs中設置的路徑。
  • backBehavior: 後退按鈕是否會致使標籤切換到初始tab? 若是是,則設切換到初始tab,不然什麼也不作。 默認爲切換到初始tab。

tabBarOptions(tab配置)

  • activeTintColor: 設置TabBar選中狀態下的標籤和圖標的顏色;
  • inactiveTintColor: 設置TabBar非選中狀態下的標籤和圖標的顏色;
  • showIcon: 是否展現圖標,默認是false;
  • showLabel: 是否展現標籤,默認是true;
  • upperCaseLabel - 是否使標籤大寫,默認爲true。
  • tabStyle: 設置單個tab的樣式;
  • indicatorStyle: 設置 indicator(tab下面的那條線)的樣式;
  • labelStyle: 設置TabBar標籤的樣式;
  • iconStyle: 設置圖標的樣式;
  • style: 設置整個TabBar的樣式;
  • allowFontScaling: 設置TabBar標籤是否支持縮放,默認支持;
  • pressColor -Color for material ripple(僅支持 Android >= 5.0;
  • pressOpacity -按下標籤時的不透明度(支持 iOS 和 Android < 5.0);
  • scrollEnabled -是否支持 選項卡滾動

eg:數組

tabBarOptions: {
  labelStyle: {
    fontSize: 12,
  },
  tabStyle: {
    width: 100,
  },
  style: {
    backgroundColor: 'blue',
  },
}
複製代碼

navigationOptions(屏幕導航選項)

createMaterialTopTabNavigator支持的屏幕導航選項的參數有:bash

  • title: 能夠用做headerTitle和tabBarLabel的備選的通用標題。
  • swipeEnabled:是否容許tab之間的滑動切換,默認容許;
  • tabBarIcon: 設置TabBar的圖標;
  • tabBarLabel: 設置TabBar的標籤;
  • tabBarOnPress: Tab被點擊的回調函數,它的參數是一保函一下變量的對象:
    • navigation:頁面的 navigation props
    • defaultHandler: tab press 的默認 handler
  • tabBarAccessibilityLabel:選項卡按鈕的輔助功能標籤。 當用戶點擊標籤時,屏幕閱讀器會讀取這些信息。 若是您沒有選項卡的標籤,建議設置此項;
  • tabBarTestID:用於在測試中找到該選項卡按鈕的 ID;

【案例1】使用createMaterialTopTabNavigator作界面導航、配置navigationOptions

createBottomTabNavigator

第一步:建立一個createMaterialTopTabNavigator類型的導航器

export const MaterialTopTabNavigator = createMaterialTopTabNavigator({//在這裏配置頁面的路由
        Page1: {
            screen: Page1,
            navigationOptions: {
                tabBarLabel: 'Page10',
                tabBarIcon: ({tintColor, focused}) => (
                    <Ionicons
                        name={'ios-home'}
                        size={26}
                        style={{color: tintColor}}
                    />
                ),
            }
        },
        Page4: {
            screen: Page4,
            navigationOptions: {
                tabBarLabel: 'Page2',
                tabBarIcon: ({tintColor, focused}) => (
                    <Ionicons
                        name={'ios-people'}
                        size={26}
                        style={{color: tintColor}}
                    />
                ),
            }
        },
        Page3: {
            screen: Page3,
            navigationOptions: {
                tabBarLabel: 'Page3',
                tabBarIcon: ({tintColor, focused}) => (
                    <Ionicons
                        name={'ios-chatboxes'}
                        size={26}
                        style={{color: tintColor}}
                    />
                ),
            }
        },
    },
    {
        tabBarOptions: {
            tabStyle: {
                minWidth: 50
            },
            upperCaseLabel: false,//是否使標籤大寫,默認爲true
            scrollEnabled: true,//是否支持 選項卡滾動,默認false
            // activeTintColor: 'white',//label和icon的前景色 活躍狀態下(選中)
            // inactiveTintColor: 'gray',//label和icon的前景色 活躍狀態下(未選中)
            style: {
                backgroundColor: '#678',//TabBar 的背景顏色
            },
            indicatorStyle: {
                height: 2,
                backgroundColor: 'white',
            },//標籤指示器的樣式
            labelStyle: {
                fontSize: 13,
                marginTop: 6,
                marginBottom: 6,
            },//文字的樣式
        },
    }
)
複製代碼

第二步:配置navigationOptions:

TabNavigator的navigationOptions有兩個關鍵的屬性,tabBarLabel標籤與tabBarIcon圖標:函數

Page2: {
    screen: Page2,
    navigationOptions: {
        tabBarLabel: 'Page2',
        tabBarIcon: ({tintColor, focused}) => (
            <Ionicons
                name={focused ? 'ios-people' : 'ios-people-outline'}
                size={26}
                style={{color: tintColor}}
            />
        ),
    }
},
複製代碼

在上述代碼中使用了react-native-vector-icons的矢量圖標做爲Tab的顯示圖標,tabBarIcon接收一個React 組件,你們能夠根據須要進行定製:學習

  • tintColor: 當前狀態下Tab的顏色;
  • focused: Tab是否被選中;

第三步:界面跳轉

const {navigation} = this.props;
 ...
 <Button
    title="跳轉到頁面4"
    onPress={() => {
        navigation.navigate("Page4",{ name: 'Devio' })
    }}
/>
複製代碼

代碼解析:測試

頁面跳轉可分爲兩步:字體

    1. 獲取navigation:
    const {navigation} = this.props;
    複製代碼
    1. 經過navigate(routeName, params, action)進行頁面跳轉:
    navigation.navigate('Page2');
     navigation.navigate('Page3',{ name: 'Devio' });
    複製代碼

    這裏在跳轉到Page3的時候傳遞了參數{ name: 'Devio' }

【高級案例】react-navigation的高級應用

在使用react-navigation時每每有些需求經過簡單的配置是沒法完成的,好比:

  • 動態配置createMaterialTopTabNavigator:官方只提供了TabNavigator中的頁面的靜態配置方式,若是TabNavigator中的頁面不固定,須要動態生成那麼須要怎麼作呢?
  • 動態配置createMaterialTopTabNavigator的樣式:經過官方的文檔是沒法實現動態改變TabNavigator的樣式的,好比:修改顯示的文字,修改字體顏色,修改圖標等等;
  • 多層嵌套後路由個性化定製:createMaterialTopTabNavigator被包裹後在TabNavigator中的頁面是沒法藉助navigation跳轉到外層StackNavigator中的頁面的,這種應用場景不少,尤爲是你須要定製TabNavigator的時候;
  • 初始化傳參:如何在設置頁面的時候傳遞參數呢?

相似上述的應用場景有不少,你們能夠經過與本教程配套的最新版React Native+Redux打造高質量上線App視頻教程進行進一步學習react-navigation的更多高級應用

相關文章
相關標籤/搜索