uniapp自定義底部導航欄tabBar

在使用uniapp開發(fā)產品的時候,官方的tabBar總是滿足不了設計稿上面的要求。通常這時候都會自定義tabBar,但是自定義會犧牲一些用戶體驗,比如在切換的時候tabBar會閃爍,特別是用戶的硬件不太高端的時候閃爍得更加明顯,還有自定義tabBar使用uni.redirectTo的話切換頁面每次都會調用onLoad,頁面數(shù)據得不到緩存。

既然這樣我嘗試使用了官方tabBar的同時使用自定義的tabBar,就是在tabBar頁面使用uni.hideTabBar({})將官方的tabBar隱藏,但是有一個小缺點就是打開小程序時并且首屏是tabBar頁面會先隱藏官方tabBar再讓自定義tabBar回到底部,不過沒有關系。

首先先在pages.josn中把需要tabBar設置好:只需要將頁面路徑放進去就可以

"tabBar": {
        "selectedColor":"#79D5AD",
        "color": "#999999",
        "backgroundColor":"#ffffff",
        "borderStyle": "white",
        "height":"0px",
        "list": [{
            "pagePath":"pages/activity/activity",
            "text": " "
        },{
            "pagePath":"pages/recommendation/recommendation",
            "text": " "
        },{
            "pagePath":"pages/message/message",
            "text": " "
        },{
            "pagePath":"pages/user/user",
            "text": " "
        }]
}

下面就是tabBar組件:這樣就可以使用uni.switchTab({})路由切換tabBar頁面,:style="{'padding-bottom': paddingBottomHeight + 'rpx'}"是給iphone X以上的手機tabBar適配了一個padding-bottom;

<template>
    <cover-view class="tabbar" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}">
        <cover-view class="tabbar-item"
            v-for="(item, index) in list" 
            :key="index" 
            @click="tabbarChange(item.path)"
        >
            <cover-image class="item-img" :src="item.icon_a" v-if="current == index"></cover-image>
            <cover-image class="item-img" :src="item.icon" v-else></cover-image>
            <cover-view class="item-name" :class="{'tabbarActive': current == index}" v-if="item.text">{{item.text}}</cover-view>
        </cover-view>
    </cover-view>
</template>

<script>
export default {
    props: {
        current: String
    },
    data() {
        return {
            paddingBottomHeight: 0,  //蘋果X以上手機底部適配高度
            list: [{
                    text: '首頁',  
                    icon: '/static/images/home.png',  //未選中圖標
                    icon_a: '/static/images/home_i.png',  //選中圖片
                    path: "activity",  //頁面路徑
                },{
                    text: '分類',
                    icon: '/static/images/classify.png',
                    icon_a: '/static/images/classify_i.png',
                    path: "recommendation",
                }
                ,{
                    text: '訂單',
                    icon: '/static/images/order.png',
                    icon_a: '/static/images/order_i.png',
                    path: 'message',
                },{
                    text: '我的',
                    icon: '/static/images/me.png',
                    icon_a: '/static/images/me_i.png',
                    path: "user",
                },
            ]
        };
    },
    created() {
        let that = this;
        uni.getSystemInfo({
            success: function (res) {
                let model = ['X', 'XR', 'XS', '11', '12', '13', '14', '15'];
                model.forEach(item => {
                    //適配iphoneX以上的底部,給tabbar一定高度的padding-bottom
                    if(res.model.indexOf(item) != -1 && res.model.indexOf('iPhone') != -1) {
                        that.paddingBottomHeight = 40;
                    }
                })
            }
        });
    },
    methods: {
        tabbarChange(path) {
            uni.switchTab({
                url: path
            })
        }
    }
};

下面使用tabBar頁面引入自定義tabBar組件:記得加上uni.hideTabBar({})把官方tabBar隱藏

<template>
    <view>
        <view-tabbar :current="0"></view-tabbar>
    </view>
</template>

<script>
    import Tabbar from '@/components/tabbar.vue'
    export default {
        components: {
            'view-tabbar': Tabbar
        }, 
        onShow() {
            uni.hideTabBar({
                animation: false
            })
        },
    }
</script>
iphone X以下和安卓端的效果

iphone X以上的效果

最后上組件的完整代碼:

<template>
    <cover-view class="tabbar" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}">
        <cover-view class="tabbar-item"
            v-for="(item, index) in list" 
            :key="index" 
            @click="tabbarChange(item.path)"
        >
            <cover-image class="item-img" :src="item.icon_a" v-if="current == index"></cover-image>
            <cover-image class="item-img" :src="item.icon" v-else></cover-image>
            <cover-view class="item-name" :class="{'tabbarActive': current == index}" v-if="item.text">{{item.text}}</cover-view>
        </cover-view>
    </cover-view>
</template>

<script>
export default {
    props: {
        current: String
    },
    data() {
        return {
            paddingBottomHeight: 0,  //蘋果X以上手機底部適配高度
            list: [{
                    text: '首頁',  
                    icon: '/static/images/home.png',  //未選中圖標
                    icon_a: '/static/images/home_i.png',  //選中圖片
                    path: "activity",  //頁面路徑
                },{
                    text: '分類',
                    icon: '/static/images/classify.png',
                    icon_a: '/static/images/classify_i.png',
                    path: "recommendation",
                }
                ,{
                    text: '訂單',
                    icon: '/static/images/order.png',
                    icon_a: '/static/images/order_i.png',
                    path: 'message',
                },{
                    text: '我的',
                    icon: '/static/images/me.png',
                    icon_a: '/static/images/me_i.png',
                    path: "user",
                },
            ]
        };
    },
    created() {
        let that = this;
        uni.getSystemInfo({
            success: function (res) {
                let model = ['X', 'XR', 'XS', '11', '12', '13', '14', '15'];
                model.forEach(item => {
                    //適配iphoneX以上的底部,給tabbar一定高度的padding-bottom
                    if(res.model.indexOf(item) != -1 && res.model.indexOf('iPhone') != -1) {
                        that.paddingBottomHeight = 40;
                    }
                })
            }
        });
    },
    watch: {
        
    },
    methods: {
        tabbarChange(path) {
            uni.switchTab({
                url: path
            })
        }
    }
};
</script>

<style lang="scss" scoped>
    .tabbarActive{
        color: #79D5AD !important;
    }
    .tabbar{
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        display: flex;
        justify-content: space-around;
        align-items: center;
        width: 100%;
        height: 100rpx;
                background-color: #ffffff;
        .tabbar-item{
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100rpx;
            .item-img{
                margin-bottom: 4rpx;
                width: 46rpx;
                height: 46rpx;
            }
            .item-name{
                font-size: 26rpx;
                color: #A3A3A3;
            }
        }
    }
</style>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容