小程序自定义导航栏

小程序自定义导航栏🐤🐤

js

js 复制代码
  data: {
    statusBarHeight: wx.getSystemInfoSync().statusBarHeight, // 状态栏高度
    navBarHeight: 44, // 导航栏高度
  },
  getSystemInfo() {
    //获取当前设备信息
    wx.getSystemInfo({
      success: res => {
        // 获取胶囊按钮信息
        let menuInfo = wx.getMenuButtonBoundingClientRect();
        // 计算导航栏高度
        let navBarHeight = menuInfo.height + (menuInfo.top - res.statusBarHeight) * 2;
        this.setData({
          statusBarHeight,
          navBarHeight
        })
      }
    })
  },
    onLoad(options) {
    this.getSystemInfo()
  },

wxml

html 复制代码
<view class="page-lucky" style="padding-top: {{statusBarHeight}}px;">
  <view class="nav-title" style=" height: {{navBarHeight}}px; line-height: {{navBarHeight}}px;">
    <image src="/static/detail/arrow-left.png" class="arrow-left" bindtap="back" mode="" />
    <text>首页</text>
  </view>
</view>

scss

css 复制代码
.page-lucky {
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;

  .arrow-left {
    position: absolute;
    top: 50%;
    left: 32rpx;
    transform: translateY(-50%);
    text-align: left;
    width: 40rpx;
    height: 40rpx;
  }

  .nav-title {
    position: relative;
    text-align: center;
    width: 100vw;
  }
}

json

json 复制代码
  "navigationStyle": "custom"

iOS 端(补充)

  • 状态栏高度使用wx.getSystemInfo中的statusBarHeight单位为px;
  • 导航栏高度固定位44px;
  • 注意:iOS端导航栏高度44与小程序默认导航栏("navgationStyle": "default")保持一致,iOS端标题与胶囊按钮并不是垂直居中的,是偏靠下一点的,而Android端是垂直居中对齐的,如果iOS也想要上下居中的效果可以采用Android端的方案;

Android 端(补充)

  • 状态栏高度使用wx.getSystemInfo中的statusBarHeight单位为px;
  • 根据胶囊的位置与高度计算出导航栏的高度,与Android端默认导航栏("navigationStyle": "default")保持一致,并且Android端标题与胶囊是垂直居中对齐的

参考链接 | 图片及文字

相关推荐
chaffererdog15 小时前
uniapp开发微信小程序使用vk-uview-ui的uSearch搜索组件,在微信开发者工具中点击输入框会意外触发custom事件
微信小程序·小程序·uni-app
2501_9159184121 小时前
Flutter 加固方案全解析,从 Dart 层到 IPA 成品的多工具协同防护体系
flutter·macos·ios·小程序·uni-app·cocoa·iphone
我命由我1234521 小时前
微信小程序 - 内容弹出框实现(Vant Weapp 实现、原生实现)
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
小码哥0681 天前
陪诊小程序核心功能拆解:预约、导航与提醒
小程序·项目源码·预约系统·陪诊·陪诊系统开发
q_19132846951 天前
基于Springboot+uniapp的智慧停车场收费小程序
java·vue.js·spring boot·小程序·uni-app·毕业设计·计算机毕业设计
2501_916007471 天前
深入理解 iOS 文件管理体系,从沙盒结构到多工具协同的工程化文件管理实践
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张1 天前
iOS 性能检测工具深度解析 多工具协同下的全维度性能检测体系建设
android·ios·小程序·https·uni-app·iphone·webview
说私域1 天前
天猫卖家运营突围:基于开源AI智能名片链动2+1模式S2B2C商城小程序的转型路径研究
人工智能·小程序·开源
00后程序员张1 天前
Fiddler调试工具全面解析 HTTPHTTPS抓包、代理设置与接口测试实战教程
前端·测试工具·ios·小程序·fiddler·uni-app·webview
Alex813201 天前
scheme跳转小程序 打不开知道指定页
小程序