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>
相关推荐
lili-felicity6 分钟前
React Native for Harmony:订单列表页面状态筛选完整实现
react native·react.js·harmonyos
wayne2149 分钟前
Zustand在ReactNative中的工程实践与性能优化总结
javascript·react native·react.js
lynn8570_blog33 分钟前
关于compose的remember
android·kotlin
毕设源码-邱学长44 分钟前
【开题答辩全过程】以 基于安卓的外卖点餐APP的设计与实现为例,包含答辩的问题和答案
android
lili-felicity1 小时前
React Native 鸿蒙跨平台开发:纯原生IndexBar索引栏 零依赖 快速定位列表
react native·react.js·harmonyos
csj501 小时前
安卓基础之《(16)—内容提供者(2)使用内容组件获取通讯信息》
android
·云扬·1 小时前
ClickHouse常用管理语句汇总:会话、磁盘、性能与复制管理
android·clickhouse
游戏开发爱好者82 小时前
2025年iOS应用上架App Store全指南,开发者必看
android·ios·小程序·https·uni-app·iphone·webview
a3158238062 小时前
Android CardView修改背景阴影
android·cardview·修改背景
kk哥88992 小时前
Android UI 优化指南:流畅度与体验双提升
android·ui