日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

定制化你的ReactNative底部导航栏

發(fā)布時間:2023/12/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 定制化你的ReactNative底部导航栏 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

? 接觸過ReactNative(以下簡稱RN)的大概都知道,react-navigation提供了兩種開箱即用的導(dǎo)航欄組件

  • createBottomTabNavigator
  • createMaterialBottomTabNavigator

分別是這樣的

盡管官方提供了導(dǎo)航欄的開箱即用方案,但是實(shí)際開發(fā)里面,我們會遇到各種各樣的導(dǎo)航欄,各種各樣的動效,所以以上可能無法滿足我們的開發(fā)需求,我們就需要定制化的去做我們導(dǎo)航欄

例如我們UI給我的導(dǎo)航欄樣式

我的內(nèi)心: 這他么中間凸起的我怎么做,老子只是一個小前端,app很渣啊啊啊

借助可愛的google,我找到了解決方法

就是

TabBarComponent

這個api在文檔資料很少,所以想要知道怎么用只能通過網(wǎng)絡(luò)上的資源了

其中深受這篇文案的啟發(fā)

Let's Create A Custom Animated Tab Bar With React Native

這位外國友人(話說reactnative在國外似乎還有點(diǎn)火),借助動畫庫react-native-pose,完成了這樣的效果

雖然是英文博客,但是配合翻譯基本閱讀無障礙,借助他的博客,我完成了ReactNative的自定義導(dǎo)航欄,效果如下

自定義底部導(dǎo)航欄

  • 自定義底部導(dǎo)航欄是基于createBottomTabNavigator,所以我們使用這個api來創(chuàng)建底部導(dǎo)航欄
  • 指定createBottomTabNavigator的tabBarComponent
  • tabBarComponent內(nèi)部進(jìn)行底部導(dǎo)航欄的編寫
  • 增加底部導(dǎo)航器

    import React from 'react' import { createBottomTabNavigator } from 'react-navigation' import Icon from '../Common/Icon' // 自定義圖標(biāo)庫 import TabBar from '../Common/TabBar' // tabBarComponent 自定義組件 // 頁面 import Category from '../View/TabBar/Category/Category' import Main from '../View/TabBar/Main/Main' import My from '../View/TabBar/My/My' import OrderList from '../View/TabBar/OrderList/OrderList' import OnlineDoctor from '../View/TabBar/OnlineDoctor/OnlineDoctor' export default createBottomTabNavigator({// 首頁:one: {screen: Main,navigationOptions: () => {return {tabBarIcon: ({ tintColor }) => {var soureImgeif (tintColor == '#CBCBCB') {soureImge = 'main'} else {soureImge = 'mainActive'}return <Icon name={soureImge} size={26} color={tintColor} />}}}},//分類:two: {screen: Category,navigationOptions: {tabBarIcon: ({ tintColor }) => {var soureImgeif (tintColor == '#CBCBCB') {soureImge = 'Category'} else {soureImge = 'CategoryActive'}return <Icon name={soureImge} size={26} color={tintColor} />}}},//問診:three: {screen: OnlineDoctor,navigationOptions: {tabBarIcon: ({ tintColor }) => {var soureImgeif (tintColor == '#CBCBCB') {soureImge = 'onLine'} else {soureImge = 'onLineActive'}return <Icon name={soureImge} size={48} color={tintColor} />}}},// 購物籃: four: {screen: OrderList,navigationOptions: {tabBarIcon: ({ tintColor }) => {var soureImgeif (tintColor == '#CBCBCB') {soureImge = 'OrderList'} else {soureImge = 'OrderListActive'}return <Icon name={soureImge} size={26} color={tintColor} />}}},//我的:five: {screen: My,navigationOptions: () => {return {tabBarIcon: ({ tintColor }) => {var soureImgeif (tintColor == '#CBCBCB') {soureImge = 'My'} else {soureImge = 'MyActive'}return <Icon name={soureImge} size={26} color={tintColor} />}}}}},{initialRouteName: 'one', // 初始化頁面tabBarComponent: TabBar,tabBarOptions: {activeTintColor: '#F34C56',inactiveTintColor: '#CBCBCB'}} )復(fù)制代碼

    工具函數(shù)

    圖標(biāo)沒有使用圖標(biāo)庫,直接搞一個圖標(biāo)庫比較得心應(yīng)手

    ../Common/Icon.js

    import React from 'react' import { Image } from 'react-native' import { TabIcon } from './Image'const Icon = ({ name, style, size }) => {const icon = TabIcon[name]return (<Imagesource={icon}style={[{ width: size, height: size }, style]}/>) }export default Icon 復(fù)制代碼

    而對于圖片則進(jìn)行統(tǒng)一管理

    ../Common/Image.js

    /*** 所有的圖片資源都從這里統(tǒng)一管理*/ // 底部導(dǎo)航欄的圖片資源 export const TabIcon = {main: require('..'),mainActive: require('..'),Category: require('..'),CategoryActive: require('..'),onLine: require('..'),onLineActive: require('..'),OrderList: require('..'),OrderListActive: require('..'),My: require('..'),MyActive: require('..'), } 復(fù)制代碼

    自定義底部導(dǎo)航器

    萬事俱備,下面就是自定義底部導(dǎo)航器了,就和定義React組件一樣

    import React from 'react' import {View,Text,StyleSheet,TouchableOpacity,TouchableNativeFeedback,Dimensions } from 'react-native' import posed from 'react-native-pose' // react-native 動畫庫const Scaler = posed.View({ // 定義點(diǎn)擊縮放active: { scale: 1 },inactive: { scale: 0.9 } })const TabBar = props => {const {renderIcon,getLabelText,activeTintColor,inactiveTintColor,onTabPress,onTabLongPress,getAccessibilityLabel,navigation} = propsconst { routes, index: activeRouteIndex } = navigation.statereturn (<Scaler style={Styles.container}>{routes.map((route, routeIndex) => {const isRouteActive = routeIndex === activeRouteIndexconst tintColor = isRouteActive ? activeTintColor : inactiveTintColorreturn (<TouchableNativeFeedbackkey={routeIndex}style={Styles.tabButton}onPress={() => {onTabPress({ route })}}onLongPress={() => {onTabLongPress({ route })}}accessibilityLabel={getAccessibilityLabel({ route })}>{route.key == 'three' ? ( // 對特殊圖標(biāo)進(jìn)行特殊處理<Scalerstyle={Styles.scalerOnline}pose={isRouteActive ? 'active' : 'inactive'}>{renderIcon({ route, focused: isRouteActive, tintColor })}<Text style={Styles.iconText}>{getLabelText({ route })}</Text></Scaler>) : ( // 普通圖標(biāo)普通處理<Scalerstyle={Styles.scaler}pose={isRouteActive ? 'active' : 'inactive'}>{renderIcon({ route, focused: isRouteActive, tintColor })}<Text style={Styles.iconText}>{getLabelText({ route })}</Text></Scaler>)}</TouchableNativeFeedback>)})}</Scaler>) }const Styles = StyleSheet.create({container: {flexDirection: 'row',height: 53,borderWidth: 1,borderRadius: 1,borderColor: '#EEEEEE',shadowOffset: { width: 5, height: 10 },shadowOpacity: 0.75,elevation: 1},tabButton: {flex: 1,justifyContent: 'center',alignItems: 'center'},spotLight: {width: tabWidth,height: '100%',justifyContent: 'center',alignItems: 'center'},spotLightInner: {width: 48,height: 48,backgroundColor: '#ee0000',borderRadius: 24},scaler: {flex: 1,alignItems: 'center',justifyContent: 'center',},scalerOnline: {flex: 1,alignItems: 'center',justifyContent: 'flex-end',},iconText: {fontSize: 12,lineHeight: 20} })export default TabBar 復(fù)制代碼

    最后實(shí)現(xiàn)的效果就是

    如果你也有這樣的需求,可以看看老外發(fā)布的那篇博客

    Let's Create A Custom Animated Tab Bar With React Native

    最后: 快要過年了,祝大家新年快樂

    總結(jié)

    以上是生活随笔為你收集整理的定制化你的ReactNative底部导航栏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。