uniapp自定义navigationBar

最终效果:

一、关闭默认导航栏

pages.json文件中,对单个页面关闭

"navigationStyle": "custom"

"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "控制台",
				"navigationStyle": "custom"
			}
		},
		{
			"path" : "pages/message/message",
			"style" : 
			{
				"navigationBarTitleText" : "消息"
			}
		}
		
	],

二、组件设计

<template>
	<view>
		<!-- 状态栏占位 -->
		<view :style="{paddingTop: statusBarHeight + 'px'}"></view>
		<!-- 导航栏内容 -->
		<view :style="{height: navBarHeight + 'px'}" class="navBarComponent">
			<view class="navContent">
				<text class="title">{{titles}}</text>
				<view class="right" :style="{width: count*40 + 'px'}" v-if="count > 0" >
					<view class="item" v-for="(item, index) in icons" :key="index">
						<image :src="item.icon" class="imageShow" @click="toPage(item.link)"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"navigationBar",
		data() {
			return {
				statusBarHeight: 0,
				navBarHeight: 0,
				count: 0,
				icons: []
			};
		},
		props:{
			titles: String,
			links: Array
		},
		methods: {
			toPage(url){
				uni.navigateTo({
					url:url
				})
			}
		},
		created() {
			// 动态获取手机状态栏高度(电量显示区域),H5没有状态栏,会为0
			this.statusBarHeight = uni.getSystemInfoSync()['statusBarHeight'];
			// #ifdef H5
			this.navBarHeight = 45
			// #endif
			// #ifndef H5
			this.navBarHeight = this.statusBarHeight + 11
			// #endif
			
			if(typeof this.$props.links === 'undefined'){
				this.count = 0
			}else{
				this.count = this.$props.links.length
				this.icons = this.$props.links
			}
		}
	}
</script>

<style lang="scss">
	.navBarComponent {
		display: flex;
		align-items: center;
		box-sizing: border-box;
		
		.navContent{
			display: flex;
			align-items: center;
			padding-left: 15px;
			padding-right: 15px;
			width: 100%;
			.title{
				font-size: 24px;
				flex: 1;
			}
			.right{
				display: flex;
				width: 120px;
				height: 100%;
				align-items: center;
				justify-content: space-between;
				.item{
					background-color: white;
					width: 36px;
					height: 36px;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					.imageShow{
						width: 60%;
						height: 60%;
					}
				}
			}
		}
	}
</style>

三、使用

<navigationBar :titles="'控制台'" :links="icons"></navigationBar>

图标链接数据:

this.icons = [
	{
	    icon: '/static/icon/notify.png',
	    link: '/pages/message/message'
	},
	{
		icon: '/static/icon/scan.png',
		link: '/pages/message/message'
	}
]

不同框架下的语法会有区别

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在uniapp自定义tabbar的样式,可以按照以下步骤进行操作: 1. 打开项目根路径下的App.vue文件 2. 在文件中增加以下代码: ```html <style lang="scss"> uni-tabbar { .uni-tabbar { // 设置tab背景 background-image: linear-gradient(to top, #FAF8F8, #FFFFFF)!important; // 若需要使用背景图片,或者渐变色,背景色和背景图片最多选择一个进行设置 .uni-tabbar-border { // 设置tabBar上边框的颜色 background-color: #F7F4F4!important; } .uni-tabbar__bd { // 设置tabBar单项的样式 .uni-tabbar__icon { // 设置图标大小 width: 48upx!important; height: 48upx!important; } .uni-tabbar__label { // 设置文字大小 font-size: 20upx!important; } } } } </style> ``` 3. 保存文件后,即可自定义tabbar的样式。 这样,你就可以根据自己的需求,通过修改对应的样式来实现uniapp自定义tabbar的样式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uniapp自定义tabbar图标样式](https://download.csdn.net/download/weixin_45826772/86754568)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [uniapp 修改tabBar图标大小和navigationBar字体大小(转载,仅作记录)](https://blog.csdn.net/Gabriel_wei/article/details/124919599)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [uniapp自定义tabbar](https://blog.csdn.net/TYD0313/article/details/127340852)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值