react native 如何实现这样的底部导航栏 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
owen800q
V2EX    程序员

react native 如何实现这样的底部导航栏

  •  
  •   owen800q 2023 年 10 月 24 日 1812 次点击
    这是一个创建于 812 天前的主题,其中的信息可能已经有所发展或是发生改变。

    react navigation bottom tab 如何实现中间的 tab 凹下去,二维码在导航栏外

    Screenshot 2023-10-24 at 12.06.30 AM.png

    2 条回复    2023-10-24 00:30:37 +08:00
    razor1895
        2
    razor1895  
       2023 年 10 月 24 日
    这是一个精简版本的实现

    import { useEffect } from 'react';
    import { Platform, StyleSheet, TouchableOpacity, View } from 'react-native';
    import FastImage from 'react-native-fast-image';
    import { trigger } from 'react-native-haptic-feedback';

    import { BlurView } from '@react-native-community/blur';
    import { BottomTabBarProps } from '@react-navigation/bottom-tabs';
    import { useQueryClient } from '@tanstack/react-query';

    import { TAB_BAR_HEIGHT } from '@/styles';
    import { p2d } from '@/utils';
    import { pubsub } from '@/utils/pubsub';

    import { Badge } from './Badge';

    const TabBarIcOnMap= [
    {
    inactive: require('@/assets/icons/icon-crushes.png'),
    active: require('@/assets/icons/icon-crushes-focused.png'),
    style: {
    width: p2d(24),
    height: p2d(17),
    },
    },
    {
    inactive: require('@/assets/icons/icon-news.png'),
    active: require('@/assets/icons/icon-news-focused.png'),
    style: {
    width: p2d(23),
    height: p2d(18.596),
    },
    },
    {
    inactive: require('@/assets/icons/icon-confessions.png'),
    active: require('@/assets/icons/icon-confessions-focused.png'),
    style: {
    width: p2d(18.35),
    height: p2d(17),
    },
    },
    {
    inactive: require('@/assets/icons/icon-profile.png'),
    active: require('@/assets/icons/icon-profile-focused.png'),
    style: {
    width: p2d(11),
    height: p2d(17),
    },
    },
    ];

    const TabBarIcon = ({
    focused,
    index,
    tabBarBadge,
    }: {
    focused: boolean;
    index: number;
    tabBarBadge?: string | number;
    }) => {
    const map = TabBarIconMap[index];
    return (
    <View>
    <FastImage
    source={focused ? map.active : map.inactive}
    style={map.style}
    resizeMode='contain'
    />
    <Badge visible={!!tabBarBadge} style={styles.badge}>
    {tabBarBadge}
    </Badge>
    </View>
    );
    };

    let showedInviteModal = false;

    export function TabBar({ state, descriptors, navigation }: BottomTabBarProps) {
    // @ts-ignore
    const bottom = global.bottomInset || 0;
    const queryClient = useQueryClient();

    useEffect(() => {
    const disposer = pubsub.subscribe('have_shown_swipe_modal', () => {
    showedInviteModal = true;
    });

    return disposer;
    }, []);

    return (
    <View style={[styles.container, { bottom: Math.max(bottom, 10) }]}>
    {Platform.OS === 'ios' && (
    <BlurView blurType='xlight' blurAmount={2} style={StyleSheet.absoluteFillObject} />
    )}
    {state.routes.map((route, index) => {
    const { options } = descriptors[route.key];

    const isFocused = state.index === index;

    const OnPress= () => {
    const event = navigation.emit({
    type: 'tabPress',
    target: route.key,
    canPreventDefault: true,
    });
    trigger('impactMedium');

    if (!isFocused && !event.defaultPrevented) {
    // The `merge: true` option makes sure that the params inside the tab screen are preserved
    navigation.navigate({ name: route.name, merge: true });
    }
    };

    return (
    <TouchableOpacity
    key={String(index)}
    accessibilityRole='button'
    accessibilityState={isFocused ? { selected: true } : {}}
    accessibilityLabel={options.tabBarAccessibilityLabel}
    testID={options.tabBarTestID}
    OnPress={onPress}
    style={styles.item}
    >
    <TabBarIcon focused={isFocused} index={index} tabBarBadge={options.tabBarBadge} />
    </TouchableOpacity>
    );
    })}
    </View>
    );
    }

    const styles = StyleSheet.create({
    container: {
    position: 'bsolute',
    // bottom: 0,
    // left: 0,
    height: TAB_BAR_HEIGHT,
    flexDirection: 'row',
    // backgroundColor: 'transparent',
    backgroundColor: 'rgba(255,255,255,0.3)',
    width: p2d(234),
    alignSelf: 'center',
    alignItems: 'center',
    borderRadius: p2d(10),
    overflow: 'hidden',
    },
    item: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    },
    icon: {
    width: p2d(18),
    height: p2d(18),
    },
    badge: {
    position: 'absolute',
    right: p2d(-5),
    top: p2d(-4),
    },
    });

    自己实现 ui 即可
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3080 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 14:14 PVG 22:14 LAX 06:14 JFK 09:14
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86