uniapp中设置TabBar

效果图:

结构目录:

pages.json:

{
	"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "首页",
				"enablePullDownRefresh": false
			}
		}, {
			"path": "pages/about/about",
			"style": {
				"navigationBarTitleText": "about页面",
				"enablePullDownRefresh": false
			}
		}
    ],
	"globalStyle": {
		"navigationBarTextStyle": "black", //white/black
		"navigationBarTitleText": "测试",
		"navigationBarBackgroundColor": "yellow"
	},
	"tabBar": {
		"selectedColor": "#0000ff",
		"color": "#C0C0C0",
		"list": [{
			"text": "首页",
			"pagePath": "pages/index/index",
			"iconPath": "static/images/index_like.png",
			"selectedIconPath": "static/images/index_like_c.png"

		}, {
			"text": "关于",
			"pagePath": "pages/about/about",
			"iconPath": "./static/images/index_address.png",
			"selectedIconPath": "static/images/index_address_c.png"
		}]
	}
}

index.vue:

<template>
	<view>
		<button @click="setTabBarStyle1">setTabBarStyle1</button>
		<button @click="setTabBarStyle2">setTabBarStyle2</button>
		<button @click="setTabBarBadge">setTabBarBadge设置tab徽标</button>
		<button @click="removeTabBarBadge">removeTabBarBadge移除tab徽标</button>
		<button @click="showTabBarRedDot">showTabBarRedDot显示红点</button>
		<button @click="hideTabBarRedDot">hideTabBarRedDot移除红点</button>
		<button @click="setTabBarItem1">setTabBarItem1自定义Tab样式1</button>
		<button @click="setTabBarItem2">setTabBarItem2自定义Tab样式2</button>
		<button @click="hideTabBar">hideTabBar隐藏TabBar</button>
		<button @click="showTabBar">showTabBar显示TabBar</button>
	</view>
</template>
<script>
export default {
	data() {
		return {};
	},
	methods: {
		setTabBarStyle1() {
			uni.setTabBarStyle({
				color: '#FFF',
				selectedColor: '#007AFF',
				backgroundColor: '#000000',
				borderStyle: 'black'
			});
		},
		setTabBarStyle2() {
			uni.setTabBarStyle({
				color: '#7A7E83',
				selectedColor: '#007AFF',
				backgroundColor: '#F8F8F8',
				borderStyle: 'black'
			});
		},
		setTabBarBadge() {
			uni.setTabBarBadge({
				index: 1,
				text: '2'
			});
		},
		removeTabBarBadge() {
			uni.removeTabBarBadge({
				index: 1
			});
		},
		showTabBarRedDot() {
			uni.showTabBarRedDot({
				index: 1
			});
		},
		hideTabBarRedDot() {
			uni.hideTabBarRedDot({
				index: 1
			});
		},
		setTabBarItem1() {
			let tabBarOptions = {
				index: 0,
				text: 'home',
				iconPath: 'static/images/index_like.png',
				selectedIconPath: 'static/images/index_like_c.png'
			};
			uni.setTabBarItem(tabBarOptions);
		},
		setTabBarItem2() {
			let tabBarOptions = {
				index: 0,
				text: '首页',
				iconPath: 'static/images/index_like.png',
				selectedIconPath: 'static/images/index_like_c.png'
			};
			uni.setTabBarItem(tabBarOptions);
		},
		showTabBar() {
			uni.showTabBar();
		},
		hideTabBar() {
			uni.hideTabBar();
		}
	}
};
</script>

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
UniApp,我们可以使用动态设置的方式来修改Tabbar的样式和内容。 一、修改Tabbar样式: 1. 在App.vue文件,添加一个全局的tabbar样式变量,例如: ```javascript export default { globalData: { tabBarStyle: { backgroundColor: '#ffffff', color: '#000000', selectedColor: '#ff0000', borderStyle: 'white', list: [ { pagePath: 'pages/index/index', text: '首页', iconPath: 'static/tabBar/home.png', selectedIconPath: 'static/tabBar/home_selected.png' }, { pagePath: 'pages/category/category', text: '分类', iconPath: 'static/tabBar/category.png', selectedIconPath: 'static/tabBar/category_selected.png' }, // 其他tabbar项... ] } } } ``` 2. 在页面的onLoad钩子函数,动态修改tabbar样式: ```javascript onLoad() { this.globalData.tabBarStyle.backgroundColor = '#f5f5f5'; this.globalData.tabBarStyle.color = '#666666'; this.globalData.tabBarStyle.selectedColor = '#ff9900'; } ``` 3. 修改完样式后,通过this.globalData.tabBarStyle.list属性可以获取到tabbar的列表数据,根据实际需要修改每个tabbar项的页面路径、文字、图标等。 二、修改Tabbar内容: 1. 在页面的onLoad钩子函数,动态修改tabbar的内容: ```javascript onLoad() { this.globalData.tabBarStyle.list[0].text = 'New首页'; this.globalData.tabBarStyle.list[0].iconPath = 'static/tabBar/home_new.png'; this.globalData.tabBarStyle.list[0].selectedIconPath = 'static/tabBar/home_selected_new.png'; } ``` 2. 修改完内容后,可以通过this.globalData.tabBarStyle.list属性获取到每个tabbar项的具体内容,并进行修改。 通过以上方式,我们可以实现在UniApp动态设置Tabbar的样式和内容。如果需要实时更新Tabbar的样式和内容,可以通过监听数据变化,动态更新页面显示。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值