微信小程序自定义导航栏;uni-app微信小程序自定义顶部导航栏;微信小程序顶部导航栏高度;微信小程序左上角胶囊按钮;uni-app微信小程序左上角胶囊设置

本文是自定义微信小程序的顶部导航栏;自定义微信小程序底部导航栏tabBar看另外这篇

文末的两个文件代码可以直接复制使用

自定义导航栏页图
在这里插入图片描述在这里插入图片描述

一、场景: 有些时候,微信小程序需要我们在导航栏做更多的操作样式(例如高度、颜色、加按钮);原生的导航栏明显无法处理,这时候我们就需要自定义导航栏;> 一、注意点:顶部导航栏高度 = 手机系统状态栏高度 + 44px
1.手机系统状态栏高度: 既你的手机最上方显示 时间、信号、电量的部分;每个手机的状态栏高度是不一样的,我们可以在onReady函数内,使用uni.getSystemInfo()获取系统信息得到statusBarHeight的高度就是(下图青绿色部分);切记:不可以使用css变量–status-bar-height,这个变量是固定的25px(会导致不同手机的状态栏不兼容)
2.紫色部分就是书写导航栏标题的位置;这个高度在微信小程序里是固定的44px;所以我们才能得到顶部导航栏的计算公式;
在这里插入图片描述

二、自定义导航栏设置后遗留问题:
–2.1自定义后,左上角的返回按钮没有了(需要自己写逻辑和按钮图标);
–2.2真机页面下划时候会导致,自定义导航栏也下滑(需要自己进行顶部导航栏固定定位fiexd和z-index);
–2.3导航栏的文字大小和颜色会和原生的不一致(原生的是跟随系统),但是可以自定义自己导航栏的文字大小和颜色;
–2.4uni-app文档的自定义导航栏使用注意事项
在这里插入图片描述

三、代码逻辑:分别是pages.json内去掉原生导航栏 和 App.vue页面储存数据 和 自定义导航栏页面
以下两个文件代码可直接复制使用,无需更改就可以运行

–步骤3.1:在pages.json内去掉原生导航栏 "navigationStyle": "custom"
在这里插入图片描述

–步骤3.2:App.vue:在onShow页面获取系统数据并存储

<script>
export default {

  onShow: function () {
    console.log('App Show')
    uni.getSystemInfo({
      success: (result) => {
        // 获取手机系统的状态栏高度(不同手机的状态栏高度不同)  ( 不要使用uni-app官方文档的var(--status-bar-height) 官方这个是固定的20px  不对的 )
        // console.log('当前手机的状态栏高度',result.statusBarHeight)
        let statusBarHeight = result.statusBarHeight + 'px'

        // 获取右侧胶囊的信息 单位px
        const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
        //bottom: 胶囊底部距离屏幕顶部的距离
        //height: 胶囊高度
        //left:   胶囊左侧距离屏幕左侧的距离
        //right:  胶囊右侧距离屏幕左侧的距离
        //top:    胶囊顶部距离屏幕顶部的距离
        //width:  胶囊宽度
        // console.log(menuButtonInfo.width, menuButtonInfo.height, menuButtonInfo.top)
        // console.log('计算胶囊右侧距离屏幕右边距离', result.screenWidth - menuButtonInfo.right)
        let menuWidth = menuButtonInfo.width + 'px'
        let menuHeight = menuButtonInfo.height + 'px'
        let menuBorderRadius = menuButtonInfo.height / 2 + 'px'
        let menuRight = result.screenWidth - menuButtonInfo.right + 'px'
        let menuTop = menuButtonInfo.top + 'px'
        let contentTop = result.statusBarHeight + 44 + 'px'

        let menuInfo = {
          statusBarHeight: statusBarHeight,//状态栏高度----用来给自定义导航条页面的顶部导航条设计padding-top使用:目的留出系统的状态栏区域
          menuWidth: menuWidth,//右侧的胶囊宽度--用来给自定义导航条页面的左侧胶囊设置使用
          menuHeight: menuHeight,//右侧的胶囊高度--用来给自定义导航条页面的左侧胶囊设置使用
          menuBorderRadius: menuBorderRadius,//一半的圆角--用来给自定义导航条页面的左侧胶囊设置使用
          menuRight: menuRight,//右侧的胶囊距离右侧屏幕距离--用来给自定义导航条页面的左侧胶囊设置使用
          menuTop: menuTop,//右侧的胶囊顶部距离屏幕顶部的距离--用来给自定义导航条页面的左侧胶囊设置使用
          contentTop: contentTop,//内容区距离页面最上方的高度--用来给自定义导航条页面的内容区定位距离使用
        }
        uni.setStorageSync('menuInfo', menuInfo)
      },
      fail: (error) => {
        console.log(error)
      }
    })
  },


}
</script>


–步骤3.3:自定义导航栏页面:

<template>
  <view class="page_box">

    <!-- 行内式直接变量小程序不支持,故需要写成动态的变量 -->
    <view class="my_tab_title" :style="{paddingTop:statusBarHeight}">
      自定义的顶部导航条

      <!-- 左侧自定义胶囊 -->
      <view class="menu_btn" :style="{ position: 'fixed',top:menuTop,left:menuRight,width:menuWidth,height:menuHeight, border: '1rpx solid #ddd',borderRadius:menuBorderRadius,}">
        <uni-icons @click="goToBack" class="arrowleft" type="arrowleft" :color="'#000'" size="20" />
        <text class="text_box"></text>
        <uni-icons @click="goToHome" class="home" type="home" :color="'#000'" size="20" />
      </view>

    </view>

    <!-- 内容区↓ ↓ ↓ ↓ ↓ ↓ -->
    <view class="content_box" :style="{marginTop:contentTop}">
      页面的正常内容书写区
    </view>

  </view>
</template>
<script>
export default {
  data () {
    return {
      statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,//状态栏的高度(可以设置为顶部导航条的padding-top)
      menuWidth: uni.getStorageSync('menuInfo').menuWidth,
      menuHeight: uni.getStorageSync('menuInfo').menuHeight,
      menuBorderRadius: uni.getStorageSync('menuInfo').menuBorderRadius,
      menuRight: uni.getStorageSync('menuInfo').menuRight,
      menuTop: uni.getStorageSync('menuInfo').menuTop,
      contentTop: uni.getStorageSync('menuInfo').contentTop,
    }
  },
  methods: {
    goToBack () {
      uni.navigateBack({
        delta: 1
      })
    },
    goToHome () {
      uni.switchTab({
        url: '/pages/index/index'
      })
      //下方方法也可以回到首页 还可以传参 但是不建议使用---因为此方法会关闭其他所有页面(导致再次打开别的页面时候 会再次触发别的页面的onLoad)
      // uni.reLaunch({
      //   url: '/pages/index/index?id=1'
      // })
    }
  },
};
</script>

<style lang="less" scope>
.page_box {
  .my_tab_title {
    width: 100%;
    height: 44px; //这个是固定的44px(所有小程序顶部高度都是 = 44px + 手机系统状态栏高度)
    line-height: 44px;
    text-align: center;
    // background-color: #f1f;
    background-color: #f8f8f8;
    position: fixed;
    top: 0;
    z-index: inherit;
    font-family: Monospaced Number, Chinese Quote, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, PingFang SC, Hiragino Sans GB, Microsoft YaHei,
      Helvetica Neue, Helvetica, Arial, sans-serif !important;
    font-size: 32rpx;
    color: #000;
    font-weight: 500;

    .menu_btn {
      background-color: #ffffff; //这个是小程序默认的标题栏背景色
      overflow: hidden;
      // position: fixed;//行内式写了固定定位--目的是去掉下划页面一起滚动问题
      .arrowleft {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-160%, -50%) !important;
        -webkit-transform: translate(-160%, -50%) !important;
      }
      .text_box {
        width: 1rpx;
        height: 20px;
        background-color: #ddd;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%) !important;
        -webkit-transform: translate(-50%, -50%) !important;
      }
      .home {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(60%, -50%) !important;
        -webkit-transform: translate(60%, -50%) !important;
      }
    }
  }
}
</style>

  • 19
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
### 回答1: Uniapp是一款多端开发框架,支持开发微信小程序、H5、App等多种平台的应用程序。在微信小程序中,往往需要自定义导航栏以满足用户需求,Uniapp提供了方便易用的方法来实现此功能。 首先,在Uniapp中创建一个自定义导航栏的组件,可以使用vue组件进行实现。在此组件中,需要定义导航栏的样式,例如背景颜色、文字颜色、边框等。 其次,在每个需要自定义导航栏的页面中引入这个组件,并将页面的标题传递给组件。这样,在页面中就可以显示出我们定义的自定义导航栏了。 除此之外,我们还可以在这个组件中添加返回按钮、菜单按钮等功能,以提升用户体验。同时,还可以通过uni.getSystemInfoSync()方法获取系统信息来动态设置导航栏高度,以兼容不同设备的屏幕尺寸。 总之,Uniapp提供了方便简洁的方法来实现自定义导航栏,在微信小程序开发中非常实用。通过此功能,我们可以为用户提供更加个性化、更加舒适的应用体验。 ### 回答2: Uniapp 是一款跨平台的开发框架,可以快速开发出支持多种小程序平台的应用,包括微信小程序。在微信小程序中,自定义导航栏能够提供更好的用户体验,同时也满足了开发者个性化设计的需求。下面将介绍如何在 Uniapp自定义微信小程序导航栏。 1. 使用插件方式 Uniapp 提供了一个可以自定义导航栏的插件 uni-navigationBar,可以方便地进行开发。使用该插件的步骤如下: (1)安装 uni-navigationBar 插件 在 HBuilderX 的插件市场搜索 uni-navigationBar 并安装。或者在项目根目录下执行以下命令: npm install --save uni-navigationbar (2)在需要使用自定义导航栏的页面引入插件 在需要使用自定义导航栏的页面的 script 标签中,引入插件并注册: import uniNavigationBar from 'uni-navigationbar/vue'; Vue.use(uniNavigationBar); (3)在页面中使用自定义导航栏 可以在页面的 template 标签中,使用 uni-navigationBar 提供的组件和组件属性来实现自定义导航栏。 2. 使用自定义组件方式 如果不想使用插件,也可以自己编写一个自定义组件来实现自定义导航栏。实现的步骤如下: (1)在项目中创建自定义组件 可以使用 HBuilderX 的“创建自定义组件”功能,在项目中创建一个自定义组件。 (2)在自定义组件中编写导航栏代码 可以在自定义组件的 template 标签中编写需要自定义导航栏代码。 (3)在需要使用自定义导航栏的页面引入自定义组件 在需要使用自定义导航栏的页面的 template 标签中,引入刚才创建的自定义组件。 (4)在页面中使用自定义导航栏 可以在页面的 template 标签中,使用刚才创建的自定义组件,来实现自定义导航栏。 总的来说,Uniapp 微信小程序自定义导航栏的实现方法比较简单。可以使用插件方式或者自定义组件方式。如果你的项目已经使用 uni-navigationBar 插件,可以直接使用该插件实现自定义导航栏;如果你需要自定义更多的样式或交互效果,可以自己编写一个自定义组件。在实际开发中,可以根据项目需求和个人喜好选择适合自己的方式来实现自定义导航栏。 ### 回答3: Uniapp 是一款跨端应用开发框架,可以同时开发出运行于多个平台的应用程序。在 Uniapp 中,为了更好地适应不同平台的导航栏样式需求,Uniapp 提供了一套自定义导航栏的解决方案。本文将介绍如何使用 Uniapp自定义导航栏功能来开发微信小程序Uniapp 提供了两种自定义导航栏的方式:native 和 js 组件两种方式。 1. native 方式: 使用 native 方式,可以直接使用小程序原生的导航栏组件,Uniapp 会提供一些额外的 API、样式、事件来方便我们在开发过程中进行调整。 2. js 组件方式: 使用 js 组件方式,可以完全自定义导航栏的样式和行为,包括可以将导航栏设置为全局组件,方便在应用程序的多个页面中复用和调用。 下面我们将以 js 组件方式为例,介绍如何使用 Uniapp自定义导航栏功能来开发微信小程序。 第一步,在 App.vue 中定义自定义导航栏组件: ``` <template> <view class="app"> <!-- 引入自定义导航栏组件 --> <custom-nav-bar title="自定义导航栏"></custom-nav-bar> <!-- 页面内容 --> <router-view></router-view> </view> </template> <script> export default { components: { // 引入自定义导航栏组件 'custom-nav-bar': () => import('@/components/CustomNavBar.vue') } } </script> <style> /* 自定义导航栏样式 */ .uni-nav-bar { height: uni-status-bar-height + 44px; padding-top: uni-status-bar-height; display: flex; justify-content: center; align-items: center; background-color: #fff; color: #000; position: relative; z-index: 100; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .uni-nav-bar__title { font-size: 18px; } </style> ``` 第二步,定义自定义导航栏组件的样式和功能: ``` <template> <view class="uni-nav-bar"> <!-- 导航栏左侧返回按钮 --> <view class="uni-nav-bar__left" @tap="back"> <text class="uni-nav-bar__back">返回</text> </view> <!-- 导航栏标题 --> <view class="uni-nav-bar__title">{{ title }}</view> </view> </template> <script> export default { props: ['title'], methods: { // 导航栏左侧返回按钮点击事件 back () { uni.navigateBack() } } } </script> <style> /* 导航栏样式 */ .uni-nav-bar { height: 44px; padding: 0 16px; display: flex; justify-content: space-between; align-items: center; background-color: #fff; color: #000; position: relative; z-index: 100; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .uni-nav-bar__left { width: 70px; display: flex; flex-direction: row; justify-content: flex-start; align-items: center; } .uni-nav-bar__back { font-size: 16px; color: #000; } .uni-nav-bar__title { font-size: 18px; } </style> ``` 以上即为使用 Uniapp自定义导航栏功能来开发微信小程序的完整示例。通过以上代码,我们定义了一个自定义导航栏组件 CustomNavBar,并且在 App.vue 中引入和展示了该组件。 在实际开发过程中,我们可以根据自己的需求来调整自定义导航栏的样式和功能,比如在导航栏右侧添加按钮、增加搜索栏等等。同时,也可以将自定义导航栏组件设置为全局组件,方便在应用程序的多个页面中复用和调用。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值