React Native 基础tabBar和自定义tabBar - bottom-tabs

一、基础TabBar
javascript 复制代码
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'

// ...
const BottomTabBar = createBottomTabNavigator()

return (
    <View style={{width: '100%', height: '100%'}}>
        <BottomTabBar.Navigator
            screenOptions={({route}) => {
                console.log('res:', route)
                return {
                    tabBarIcon: ({focused, color, size}) => {
                    let name = focused ? `${route.name}Active` : route.name
                     return (
                         <Image 
                             style={{width: size, height: size, tintColor: color}}
                             source={IconTabMap[name]} />
                     )
                    }
                }
            }}  
             
        >
            <BottomTabBar.Screen
                name='Home'
                component={Home}
                options={{
                    title: '首页',
                    headerShown: false,
                    tabBarActiveTintColor: '#ff2442',
                    tabBarInactiveTintColor: '#999'
                }}
            />
            <BottomTabBar.Screen
                name='Shop'
                component={Shop}
                options={{
                    title: '购物车',
                    headerShown: false,
                    tabBarActiveTintColor: '#ff2442',
                    tabBarInactiveTintColor: '#999'
                }}
            />
            <BottomTabBar.Screen
                name='Message'
                component={Message}
                options={{
                    title: '消息',
                    headerShown: false,
                    tabBarActiveTintColor: '#ff2442',
                    tabBarInactiveTintColor: '#999'
                }}
            />
            <BottomTabBar.Screen
                name='Mine'
                component={Mine}
                options={{
                    title: '我的',
                    headerShown: false,
                    tabBarActiveTintColor: '#ff2442',
                    tabBarInactiveTintColor: '#999'
                }}
            />
        </BottomTabBar.Navigator>
    </View>
)
二、自定义TabBar
javascript 复制代码
const TabBarRender = ({state, descriptors, navigation}) => {
  // index:当前选中
  const { routes, index } = state

  return (
      <View style={styles.tabBar}>
          {
              routes.map((route, i) => {
                  // route -> {key, name, params}
                  // descriptors->{key值: {navigation, options(Screen上的options值), route, render}}
                  console.log('descriptors', descriptors[route.key])
                  const { options: { title } } = descriptors[route.key]
                  const isActive = index === i

                  if (i === 2) {
                      return (
                          <TouchableOpacity
                              activeOpacity={0.7}
                              onPress={() => {
                                  // ....
                              }}
                          >
                              <Image
                                  style={styles.img}
                                  source={IconAdd}
                              />
                          </TouchableOpacity>
                      )
                  }

                  return (
                      <TouchableOpacity
                          style={styles.tabItem}
                          key={`tab-item-${i}`}
                          activeOpacity={0.7}
                          onPress={() => {
                              navigation.navigate(route.name)
                          }}
                      >
                          <Text style={[styles.text, isActive ? styles.textActive : {}]}>
                              { title }
                          </Text>
                      </TouchableOpacity>
                  )
              })
          }
      </View>
      
  )

}



// ...
<BottomTabBar.Navigator
    tabBar={props => <TabBarRender {...props} />} // 自定义
>
    <BottomTabBar.Screen
        name='Home'
        component={Home}
        options={{
            title: '首页',
            headerShown: false,
        }}
    />
    <BottomTabBar.Screen
        name='Shop'
        component={Shop}
        options={{
            title: '购物车',
            headerShown: false,
        }}
    />
    <BottomTab.Screen
        name='Add'
        component={Shop}
        options={{
            title: '新增',
            headerShown: false,
        }}
    />
    <BottomTabBar.Screen
        name='Message'
        component={Message}
        options={{
            title: '消息',
            headerShown: false,
        }}
    />
    <BottomTabBar.Screen
        name='Mine'
        component={Mine}
        options={{
            title: '我的',
            headerShown: false,
        }}
    />
</BottomTabBar.Navigator>
相关推荐
idjl9 分钟前
Mysql测试题
android·adb
游戏开发爱好者83 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号3 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h5 小时前
Android原生Dialog
android
梁同学与Android5 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
谢尔登6 小时前
【React Native】布局和 Stack 、Slot
javascript·react native·react.js
iHero6 小时前
【Nextcloud】在 Ubuntu 22.04.3 LTS 上的 Nextcloud Hub 10 (31.0.2) 后台任务cron 的优化
android·linux·ubuntu·nextcloud
yuanlaile10 小时前
Flutter Android打包学习指南
android·flutter·flutter打包·flutter android
教程分享大师10 小时前
中兴B860AV5.1-M2_S905L3SB最新完美版线刷包 解决指示灯异常问题
android