【Uni】自定义导航栏(仿微信)

uni相关的环境搭建就不在此赘言。

先上效果图:

自定义导航需要在程序根目录下的pages.json中进行配置

{
	"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
		{
			"path": "pages/index/index",
			"style": {			
			}
		}
	],
    //全局页面样式
	"globalStyle": {
		// //导航栏标题颜色及状态栏字体颜色,仅支持 black/white
		"navigationBarTextStyle": "white",
		"app-plus": {
			//去掉原生导航栏
			 "titleNView": false  
		}

	}
}

导入阿里矢量图库

-》先在根目录下的static下面新建icon文件夹,之后将下载来的阿里矢量图库的代码文件拷贝进去。

-》在根目录下的main.js中引入

import Vue from 'vue'
import App from './App'
import './static/icon/iconfont.css'

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()


页面实现

路径pages-》index-》index.vue

<template>
	<view class="content">
		<!-- 上方显示时间 显示wifi的通栏 -->
		<view class="status_bar">
			<view class="top_view"></view>
		</view>
		<view class="nav">
			<view class="nav-title">Gecer</view>
			<view class="nav-contorls">
				<view class="iconfont icon-sousuo"></view>
				<view class="iconfont icon-tianjia" @click="isShowPop=!isShowPop"></view>
			</view>
			<view class="mune-pop" v-if="isShowPop">
				<view class="iconfont icon-tianjiahaoyou">
					添加好友
				</view>
				<view class="iconfont icon-daifukuan">
					收付款
				</view>
				<view class="iconfont icon-youjian">
					帮助与反馈
				</view>
			</view>
		</view>
		
		<view class="main">
			
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				isShowPop:false
			}
		},
		onLoad() {
			console.log('onLoad')
		},
		methods: {

		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	//上方手机状态栏 通栏
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
		background-color: #545454;
	}

	.top_view {
		height: var(--status-bar-height);
		width: 100%;
		position: fixed;
		background-color: #545454;
		top: 0;
		z-index: 999;
	}

	//导航
	.nav {
		position: relative;
		display: flex;
		width: 100%;
		height: 60px;
		justify-content: space-between;
		align-items: center;
		background: #EFEFEF;
	}

	.nav-title {
		margin-left: 10px;
		font-size: 20px;
		font-weight: bold;
	}

	//导航按钮
	.nav-contorls {
		display: flex;
	}

	.nav-contorls view:nth-child(1) {
		margin-right: 8px;
	}

	.nav-contorls view {
		padding: 10px;
		border-radius: 8px;
	}

	.nav-contorls view:active {
		background: #C0C0C0;
	}

	.nav-contorls {
		margin-right: 10px;
	}

	.nav-contorls .iconfont {
		font-size: 25px;
	}
	.mune-pop{
		position: absolute;
		top: 60px;
		right: 10px;
		width: 180px;
		
		background: #545454;
		margin-top: 5px;
		border-radius: 8px;
	}
	.mune-pop:after{
		position: absolute;
		top: -5px;
		right: 15px;
		content: '';
		height: 0;
		width: 0;
		border-bottom: 8px solid #545454;
		border-right: 9px solid transparent;
		border-left: 9px solid transparent;		
	}
	.mune-pop>view{
		color: white;
		font-size: 20px ;
		margin: 10px 0 10px 20px ;
		height: 40px;
		line-height: 40px;
		border-bottom: 1px solid #808080;
	}
	.mune-pop .iconfont:before{
		margin-right: 10px;
	}
</style>

这里需要注意 这个是uni自带的用于描述 程序上方状态栏(wifi、手机信号等)的标签

<view class="status_bar">
			<view class="top_view"></view>
		</view>

样式 

//上方手机状态栏 通栏
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
		background-color: #545454;
	}

	.top_view {
		height: var(--status-bar-height);
		width: 100%;
		position: fixed;
		background-color: #545454;
		top: 0;
		z-index: 999;
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现自定义导航栏微信胶囊按钮对齐,可以按照以下步骤进行: 1. 首先在 `App.vue` 中设置导航栏的高度为微信胶囊按钮的高度,即44px。可以使用 `uni.getSystemInfoSync()` 获取系统信息,再根据 `statusBarHeight` 和 `menuButtonBoundingClientRect.height` 计算出导航栏的高度。 ``` <template> <view class="navigation-bar" :style="{ height: navigationBarHeight + 'px' }"> <!-- 自定义导航栏内容 --> </view> </template> <script> export default { data() { return { navigationBarHeight: 0 } }, mounted() { const systemInfo = uni.getSystemInfoSync() const { statusBarHeight, menuButtonBoundingClientRect } = systemInfo this.navigationBarHeight = statusBarHeight + menuButtonBoundingClientRect.height } } </script> <style> .navigation-bar { position: fixed; top: 0; left: 0; right: 0; background-color: #fff; z-index: 999; } </style> ``` 2. 接下来,在页面中使用 `uni.getMenuButtonBoundingClientRect()` 获取微信胶囊按钮的位置信息,然后根据位置信息计算出导航栏左侧和右侧的边距。 ``` <template> <view class="navigation-bar" :style="{ height: navigationBarHeight + 'px', paddingLeft: paddingLeft + 'px', paddingRight: paddingRight + 'px' }"> <!-- 自定义导航栏内容 --> </view> </template> <script> export default { data() { return { navigationBarHeight: 0, paddingLeft: 0, paddingRight: 0 } }, mounted() { const systemInfo = uni.getSystemInfoSync() const { statusBarHeight, menuButtonBoundingClientRect } = systemInfo this.navigationBarHeight = statusBarHeight + menuButtonBoundingClientRect.height const { left, right } = uni.getMenuButtonBoundingClientRect() this.paddingLeft = left this.paddingRight = systemInfo.windowWidth - right } } </script> <style> .navigation-bar { position: fixed; top: 0; left: 0; right: 0; background-color: #fff; z-index: 999; } </style> ``` 通过以上步骤,就可以实现自定义导航栏微信胶囊按钮对齐了。需要注意的是,如果页面中使用了 `uni.setNavigationBarTitle()` 等原生导航栏相关方法,需要自行计算出对应的边距。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值