wx小程序设置沉浸式导航文字高度问题

第一步:在app.json中设置

复制代码
  "navigationStyle": "custom"

第二步骤:文件的home.js中

javascript 复制代码
// pages/test/test.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: 0,
    navBarHeight: 44 // 自定义导航内容区高度(单位px)

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { statusBarHeight } = wx.getWindowInfo()
    this.setData({
      statusBarHeight,
      // 总高度 = 状态栏高度 + 自定义导航内容高度
      navTotalHeight: statusBarHeight + this.data.navBarHeight
    })
  },

})

第三步:home.wxml页面

XML 复制代码
<!-- 自定义导航栏容器 -->
<view class="custom-nav" style="height: {{navTotalHeight}}px;">
  <!-- 状态栏占位 -->
  <view style="height: {{statusBarHeight}}px"></view>
  
  <!-- 导航内容区域(可设置任意高度) -->
  <view class="nav-content" style="height: {{navBarHeight}}px">
    <text>自定义标题</text>
   
  </view>
</view>

<!-- 页面内容(需下移避免被遮挡) -->
<view class="page-content" style="padding-top: {{navTotalHeight}}px">
  ...页面内容...
</view>

第四步:home.wxss

XML 复制代码
.custom-nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  background: rgb(230, 32, 32); /* 导航背景色 */
}

.nav-content {
  display: flex;
  align-items: center;
  padding: 0 16rpx;
}

.page-content {
  box-sizing: border-box;
}

最终结果:

相关推荐
游戏开发爱好者85 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106327 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106327 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息9 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
光影少年1 天前
AIGC + Taro / 小程序
小程序·aigc·taro
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
天空属于哈夫克31 天前
Go 语言实战:构建一个企微外部群“技术贴收藏夹”小程序后端
小程序·golang·企业微信
菜鸟una1 天前
【微信小程序+Taro 3+NutUI 3】input (nut-input) 、 textarea (nut-texteare)类型使用避坑
前端·vue.js·微信小程序·小程序·taro
计算机毕设指导61 天前
基于微信小程序的校园二手交易系统【源码文末联系】
java·spring boot·spring·微信小程序·小程序·tomcat·maven