uni-app中底部导航的实现

uni-app修炼之路(六)

参考官方文档:https://uniapp.dcloud.io/collocation/pages?id=tabbar

tabBar

tabBar 用来配置导航栏的表现,以及导航切换时显示的对应页。

属性说明:

属性类型必填默认值描述平台差异说明
colorHexColortab 上的文字默认颜色
selectedColorHexColortab 上的文字选中时的颜色
backgroundColorHexColortab 的背景色
borderStyleStringblacktabbar 上边框的颜色,可选值 black/whiteApp 2.3.4+ 支持其他颜色值
blurEffectStringnoneiOS 高斯模糊效果,可选值 dark/extralight/light/none(参考:使用说明)App 2.4.0+ 支持
listArraytab 的列表,详见 list 属性说明,最少2个、最多5个 tab
positionStringbottom 可选值 bottom、top top 值仅微信小程序支持
fontSizeString10px文字默认大小App 2.3.4+
iconWidthString24px图标默认宽度(高度等比例缩放)App 2.3.4+
spacingString3px图标和文字的间距App 2.3.4+
heightString50pxtabBar 默认高度App 2.3.4+
midButtonObject中间按钮 仅在 list 项为偶数时有效App 2.3.4+

其中 list 接收一个数组,数组中的每个项都是一个对象,其属性值如下:

属性类型必填说明
pagePathString页面路径,必须在 pages 中先定义
textStringtab 上按钮文字,在 App 和 H5 平台为非必填。例如中间可放一个没有文字的+号图标
iconPathString图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px,当 postion 为 top 时,此参数无效,不支持网络图片,不支持字体图标
selectedIconPathString选中时的图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px ,当 postion 为 top 时,此参数无效

midButton 属性说明

属性类型必填默认值描述
widthString80px中间按钮的宽度,tabBar 其它项为减去此宽度后平分,默认值为与其它项平分宽度
heightString50px中间按钮的高度,可以大于 tabBar 高度,达到中间凸起的效果
textString中间按钮的文字
iconPathString中间按钮的图片路径
iconWidthString24px图片宽度(高度等比例缩放)
backgroundImageString中间按钮的背景图片路径

midButton没有pagePath,需监听点击事件,自行处理点击后的行为逻辑。监听点击事件为调用API:uni.onTabBarMidButtonTap,详见https://uniapp.dcloud.io/api/ui/tabbar?id=ontabbarmidbuttontap

注意

  • 当设置 position 为 top 时,表示导航栏在顶部,将不会显示 icon
  • tabBar 中的 list是一个数组,只能配置最少2个、最多5个 tab,tab 按数组的顺序排序。
  • tabbar切换第一次加载时可能渲染不及时,可以在每个tabbar页面的onLoad生命周期里先弹出一个等待雪花(hello uni-app使用了此方式)
  • tabbar 的页面展现过一次后就保留在内存中,再次切换 tabbar页面,只会触发每个页面的onShow,不会再触发onLoad。
  • 顶部的 tabbar目前仅微信小程序上支持。需要用到顶部选项卡的话,建议不使用 tabbar 的顶部设置,而是自己做顶部选项卡,可参考 hello uni-app->模板->顶部选项卡。

pages.json代码示例:

{
	"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
		{
			"path": "pages/index/index",
			"style": {
				//"navigationBarTitleText": "uni-app"
			}
		}
        ,{
            "path" : "pages/news/news",
            "style" : {}
        }
        ,{
            "path" : "pages/messages/messages",
            "style" : {}
        }
        ,{
            "path" : "pages/home/home",
            "style" : {}
        }
    ],
	"globalStyle": {
		"navigationBarTextStyle": "black",
		"navigationBarTitleText": "社交app",
		"navigationBarBackgroundColor": "#F8F8F8",
		"backgroundColor": "#F8F8F8"
	},
	"tabBar":{
		"color":"#ADADAD",
		"selectedColor":"#FEE42B",
		"borderStyle":"black",
		"backgroundColor":"#FFFFFF",
		"list":[
			{
				"pagePath":"pages/index/index",
				"text":"首页",
				"iconPath":"static/tabbar/index.png",
				"selectedIconPath":"static/tabbar/indexed.png"
			},
			{
				"pagePath":"pages/news/news",
				"text":"动态",
				"iconPath":"static/tabbar/news.png",
				"selectedIconPath":"static/tabbar/newsed.png"
			},
			{
				"pagePath":"pages/messages/messages",
				"text":"消息",
				"iconPath":"static/tabbar/messages.png",
				"selectedIconPath":"static/tabbar/messagesed.png"
			},
			{
				"pagePath":"pages/home/home,
				",
				"text":"我的",
				"iconPath":"static/tabbar/home.png",
				"selectedIconPath":"static/tabbar/homeed.png"
			}
		]
	}
}

tabbar中的icon我是从阿里巴巴iconfont中下载的,不清楚的可以参考我另一篇博客uni-app引入自定义图标库——Iconfont-阿里巴巴,不同之处在于这里的图标不是用代码的方式,而是下载下来,存放在项目的static文件夹下面。
下载选中和未选中时的两种状态的颜色,下载方式:
在这里插入图片描述
在这里插入图片描述
下载后保存在项目的static目录下。
在这里插入图片描述

实现效果:
在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app是一个跨平台的开发框架,可以用于开发多个平台的应用程序。在uni-app,可以通过自定义底部导航栏实现个性化的界面设计和功能需求。 以下是一种实现自定义底部导航栏的方法: 1. 首先,创建一个自定义组件,用于显示底部导航栏的每个按钮。可以使用`uni-icons`组件来显示图标,并添加相应的点击事件。 ```vue <template> <view> <view class="tabbar"> <view class="tabbar-item" @click="navigateTo('home')"> <uni-icons type="home"></uni-icons> <text>首页</text> </view> <view class="tabbar-item" @click="navigateTo('discover')"> <uni-icons type="discover"></uni-icons> <text>发现</text> </view> <view class="tabbar-item" @click="navigateTo('mine')"> <uni-icons type="mine"></uni-icons> <text>我的</text> </view> </view> </view> </template> <script> export default { methods: { navigateTo(page) { uni.navigateTo({ url: `/pages/${page}/${page}` }); } } } </script> <style> .tabbar { display: flex; justify-content: space-around; align-items: center; height: 100px; background-color: #f5f5f5; } .tabbar-item { display: flex; flex-direction: column; align-items: center; } </style> ``` 2. 在需要显示底部导航栏的页面,引入自定义组件,并在页面的底部位置使用该组件。 ```vue <template> <view> <!-- 页面内容 --> </view> <custom-tabbar></custom-tabbar> </template> <script> import CustomTabbar from '@/components/CustomTabbar.vue'; export default { components: { CustomTabbar } } </script> ``` 通过以上步骤,你可以实现一个自定义的底部导航栏,并在不同的页面之间进行切换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值