uniapp自定义导航栏,高度,自定义组件

设置自定义导航栏 :globalStyle 内 navigationStyle": "custom"

配置页面:pages.json

"globalStyle": {
		"mp-alipay": {
			/* 支付宝小程序特有相关 */
			"transparentTitle": "always",
			"allowsBounceVertical": "NO"
		},
		"navigationBarBackgroundColor": "#0081ff",
		"navigationBarTitleText": "JEECG BOOT",
		"navigationStyle": "custom",//自定义导航栏
		"navigationBarTextStyle": "white"
	},

自定义导航栏组件
组件名:cu-custom.vue

<template>
	<view>
		<view class="cu-custom" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar fixed" :style="style" :class="[bgImage!=''?'none-bg text-white bg-img':'',bgColor]">
				<view class="action" @tap="BackPage" v-if="isBack">
					<text class="cuIcon-back"></text>
					<slot name="backText"></slot>
				</view>
				<view class="content" :style="[{top:StatusBar + 'px'}]">
					<slot name="content"></slot>
				</view>
				<view class="action">
					<slot name="right"></slot>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar
			};
		},
		name: 'cu-custom',
		computed: {
			style() {
				var StatusBar= this.StatusBar;
				var CustomBar= this.CustomBar;
				var bgImage = this.bgImage;
				var style = `height:${CustomBar}px;padding-top:${StatusBar}px;`;
				if (this.bgImage) {
					style = `${style}background-image:url(${bgImage});`;
				}
				return style
			}
		},
		props: {
			bgColor: {
				type: String,
				default: ''
			},
			isBack: {
				type: [Boolean, String],
				default: false
			},
			bgImage: {
				type: String,
				default: ''
			},
		},
		methods: {
			BackPage() {
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
</script>

设置导航栏高度自适应
修改页面:App.vue

<script>
	import Vue from 'vue'
	import appUpdate from 'common/util/appUpdate.js'
	export default {
		onLaunch: function() {
			uni.getSystemInfo({
				success: function(e) {
					
					// #ifdef APP-PLUS
					// 检测升级
					appUpdate()
					// #endif
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif		

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
					
					
				}
			})
            Vue.prototype.NavBarColor='bg-gradual-blue'
            Vue.prototype.Radio_Check_Size='scale(0.7)'
			Vue.prototype.ColorList = [{
					title: '嫣红',
					name: 'red',
					color: '#e54d42'
				},
				{
					title: '桔橙',
					name: 'orange',
					color: '#f37b1d'
				},
				{
					title: '明黄',
					name: 'yellow',
					color: '#fbbd08'
				},
				{
					title: '橄榄',
					name: 'olive',
					color: '#8dc63f'
				},
				{
					title: '森绿',
					name: 'green',
					color: '#39b54a'
				},
				{
					title: '天青',
					name: 'cyan',
					color: '#1cbbb4'
				},
				{
					title: '海蓝',
					name: 'blue',
					color: '#0081ff'
				},
				{
					title: '姹紫',
					name: 'purple',
					color: '#6739b6'
				},
				{
					title: '木槿',
					name: 'mauve',
					color: '#9c26b0'
				},
				{
					title: '桃粉',
					name: 'pink',
					color: '#e03997'
				},
				{
					title: '棕褐',
					name: 'brown',
					color: '#a5673f'
				},
				{
					title: '玄灰',
					name: 'grey',
					color: '#8799a3'
				},
				{
					title: '草灰',
					name: 'gray',
					color: '#aaaaaa'
				},
				{
					title: '墨黑',
					name: 'black',
					color: '#333333'
				},
				{
					title: '雅白',
					name: 'white',
					color: '#ffffff'
				},
			]

		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}

	}
</script>

引用自定义组件

<template>
	<view>
		<cu-custom :bgColor="NavBarColor" :isBack="true" backRouterName="index">
			<block slot="backText">返回</block>
			<block slot="content">通讯录</block>
		</cu-custom>
	</view>
</template>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 UniApp 中自定义导航并适配高度,你可以按照以下步骤操作: 1. 在 App.vue 文件中,添加一个自定义导航组件,如下所示: ```html <template> <div> <custom-nav-bar></custom-nav-bar> <router-view></router-view> </div> </template> <script> import CustomNavBar from '@/components/CustomNavBar.vue' export default { components: { CustomNavBar } } </script> ``` 2. 在 CustomNavBar.vue 组件中,设置导航的样式,并使用 `uni.getSystemInfoSync()` 方法获取系统信息,以动态计算导航高度,如下所示: ```html <template> <div class="custom-nav-bar" :style="{ height: navBarHeight + 'px' }"> <div class="custom-nav-bar__left" @click="onLeftClick"> <slot name="left"></slot> </div> <div class="custom-nav-bar__title"> <slot></slot> </div> <div class="custom-nav-bar__right"> <slot name="right"></slot> </div> </div> </template> <script> export default { computed: { navBarHeight() { const systemInfo = uni.getSystemInfoSync() const statusBarHeight = systemInfo.statusBarHeight || 0 const navigationBarHeight = 44 const totalHeight = statusBarHeight + navigationBarHeight return totalHeight } }, methods: { onLeftClick() { uni.navigateBack() } } } </script> <style lang="scss"> .custom-nav-bar { display: flex; align-items: center; justify-content: space-between; padding: 0 16px; background-color: #ffffff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); .custom-nav-bar__left { font-size: 16px; color: #333333; } .custom-nav-bar__title { font-size: 18px; font-weight: 500; color: #333333; } .custom-nav-bar__right { font-size: 16px; color: #333333; } } </style> ``` 3. 在需要使用自定义导航的页面中,使用 `uni.getMenuButtonBoundingClientRect()` 方法获取右侧菜单按钮的位置信息,并根据此信息调整页面的样式,使页面内容不被导航遮挡,如下所示: ```html <template> <div class="page"> <custom-nav-bar> <template #left> <uni-icons type="back" size="28" color="#333333"></uni-icons> </template> <span>页面标题</span> <template #right> <uni-icons type="ellipsis" size="28" color="#333333"></uni-icons> </template> </custom-nav-bar> <div class="content"> 页面内容 </div> </div> </template> <script> export default { mounted() { const menuButtonInfo = uni.getMenuButtonBoundingClientRect() uni.getSystemInfo({ success: (res) => { const statusBarHeight = res.statusBarHeight || 0 const navBarHeight = this.$refs.navBar.clientHeight const titleBarHeight = statusBarHeight + navBarHeight const contentHeight = res.windowHeight - titleBarHeight - menuButtonInfo.height - (menuButtonInfo.top - statusBarHeight) * 2 this.$refs.content.style.height = contentHeight + 'px' } }) } } </script> <style lang="scss"> .page { height: 100%; .content { background-color: #f5f5f5; padding: 16px; overflow-y: scroll; } } </style> ``` 这样,你就可以自定义导航并适配高度了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值