微信小程序 顶部自定义导航 “navigationStyle“: “custom“ 真机iPhone6/7/8不显示胶囊按钮

微信小程序 顶部自定义导航 “navigationStyle”: “custom”

要实现这种效果图

在这里插入图片描述

1、在哪个页面上实现自定义导航栏就在哪个页面的 .json 文件中写上: “navigationStyle”: “custom” ,如果在app.json写那就是所有的页面都会变成通栏

{
 "component": true,
"navigationStyle": "custom"
}

2、页面的.wxml代码:

<view class="navbar" style="height:{{navHeight}}px">  
 <!-- 左上角 返回按钮 和 home按钮 wx:if="{{showNav}}" 是控制左上角按钮的显示隐藏,首页不显示 -->
 <view class="navbar_left"
  style="top:{{navTop}}px;height:{{jnheight}}px;width:{{jnwidth}}px"
  wx:if="{{showNav}}">
    <!-- 控制返回按钮的显示 -->
    <view bindtap="navBack">
      <image src="/pages/images/re_goodslist.png" mode="widthFix" style="width:40%"></image>
    </view>
    <!-- home按钮 wx:if="{{showHome}}" 是控制左上角 home按钮的显示隐藏-->
    <view class="nav_line" bindtap="navHome" wx:if="{{showHome}}">
      <image src="/pages/images/re_index.png" mode="widthFix" style="width:40%"></image>
    </view>
</view>
 <!-- 中间标题 -->
<view class="navbar_title" style="top:{{navTop}}px;">{{pageName}}</view>        

3、页面的.js代码:

 const App = getApp();
Component({
// 组件的属性列表
  properties: {
 pageName: String, //中间的title
	showNav: { //判断是否显示左上角的按钮    
  type: Boolean,
  value: true
},
showHome: { //判断是否显示左上角的home按钮
  type: Boolean,
  value: true
}
  },
  // 组件的初始数据
data: {
showNav: true, //判断是否显示左上角的home按钮
showHome: true, //判断是否显示左上角的按钮
},
 lifetimes: {
// 生命周期函数,可以为函数,或一个在methods段中定义的方法名
attached: function() {
  this.setData({
    navHeight: App.globalData.navHeight, //导航栏高度
    navTop: App.globalData.navTop, //胶囊按钮与顶部的距离
    jnheight: App.globalData.jnheight, //胶囊高度
    jnwidth: App.globalData.jnwidth //胶囊宽度
  })
}
},
  // 组件的方法列表
 methods: {
//回退
navBack: function() {
  wx.switchTab({
    url: '/pages/goods/goods'
  })
},
//回主页
navHome: function() {
  wx.switchTab({
    url: '/pages/index/index'
  })
},
 }
})

4、页面的.wxss代码:

.navbar {
width: 100%;
overflow: hidden;
position: fixed;
 top: 0;
 left: 0;
 z-index: 1;
 flex-shrink: 0;
 height: 0;
}
.navbar_left {
display: -webkit-flex;
display: flex;
 -webkit-box-align: center;
-ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
 position: fixed;
 left: 10px;
z-index: 999;
line-height: 1;
 border: 1rpx solid rgba(255, 255, 255, .1);
border-radius: 40rpx;
 overflow: hidden;
  background: rgba(0, 0, 0, 0.2);
}

.navbar_left view {
width: 50%;
 display: flex;
 align-items: center;
  justify-content: center;
 z-index: 999;
}

.nav_line {
 border-left: 1rpx solid rgba(255, 255, 255, .9);
}

.navbar_title {
 width: 100%;
 box-sizing: border-box;
 padding-left: 115px;
padding-right: 115px;
height: 32px;
 line-height: 32px;
 text-align: center;
  position: absolute;
  left: 0;
  z-index: 10;
color: #333;
  font-size: 16px;
 font-weight: bold;
  text-overflow: ellipsis;
overflow: hidden;
 white-space: nowrap;
}

6、app.js代码:

App({
 //设置全局对象
 globalData: {
 navHeight: 0, 
 navTop: 0,     
   jnheight: 0,
   jnwidth: 0,   
  },
 onLaunch: function () {
   //设置导航栏
//获取菜单按钮的布局位置信息
 let menuButtonObject = wx.getMenuButtonBoundingClientRect();
 //获取系统信息
  wx.getSystemInfo({
   success: res => {
   //状态栏的高度
   let statusBarHeight = res.statusBarHeight,
   //胶囊按钮与顶部的距离
  navTop = menuButtonObject.top, 
  navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; 
  this.globalData.navHeight = navHeight;//导航栏高度
  this.globalData.navTop = navTop;//胶囊按钮与顶部的距离
  this.globalData.jnheight = menuButtonObject.height;//胶囊的高度
  this.globalData.jnwidth = menuButtonObject.width;//胶囊的宽度
},
fail(err) {
  console.log(err);
}
  });
  }
  }

上面就是完整的代码 以下说过我踩的一个坑

顶部给粘性定位时这块背景图就默认白色 我一开始给的粘性定位
在这里插入图片描述
我就把高度去掉 这时候可以变成通栏效果 在调试器上各种型号手机都没问题 但在真机iphone6/7/8上左边自定义的胶囊按钮就不显示了 不把高度去掉又做不成通栏效果 然后各种百度还是无果

然后第二天我看见了这个粘性定位 就试着把它改成固定定位试试
在这里插入图片描述
然后就解决了 害 我整了快一下午

<think>好的,我现在需要解决用户在uni-app开发微信小程序时遇到的导航栏和胶囊按钮布局或样式问题。首先,我需要明确用户的具体需求。导航栏通常包括标题和胶囊按钮,而胶囊按钮的位置和样式在同设备上可能会有差异,特别是在同屏幕尺寸或系统版本下。用户可能遇到了导航栏高度适配、胶囊按钮位置偏移或者样式一致的问题。 接下来,我需要回忆uni-app和微信小程序的相关文档。微信小程序胶囊按钮位置是固定的,由微信客户端控制,开发者无法直接修改其位置或样式。但可以通过API获取胶囊按钮的坐标信息,从而调整导航栏的布局。uni-app作为一个跨平台框架,可能需要使用条件编译来处理微信小程序特有的逻辑。 然后,我应该考虑如何获取胶囊按钮的位置信息。根据微信小程序文档,可以使用`wx.getMenuButtonBoundingClientRect()`方法获取胶囊按钮的尺寸和位置。这个API返回的top、right、bottom、left等属性可以帮助计算出导航栏的高度和布局。 导航栏的高度计算需要注意状态栏的高度加上胶囊按钮的高度。状态栏高度可以通过`wx.getSystemInfoSync().statusBarHeight`获取。胶囊按钮的高度通常是32px,但同设备可能有差异,所以最好动态获取。 接下来,用户可能需要将这些信息应用到uni-app的页面结构中。比如,在页面的最顶部设置一个占位视图,高度等于导航栏总高度,确保页面内容会被遮挡。同时,自定义导航栏的样式需要覆盖原生导航栏,这需要在pages.json中设置`"navigationStyle": "custom"`。 在样式方面,可能需要使用绝对定位或固定定位来放置自定义导航栏的元素,确保其与胶囊按钮对齐。同时,需要注意同设备的适配,使用rpx单位来保证样式的一致性。 此外,用户可能会遇到自定义导航栏中的元素点击区域与胶囊按钮冲突的问题。这时候需要通过计算胶囊按钮的位置,调整自定义元素的布局,避免重叠。 最后,需要提醒用户在测试时使用真机预览,因为开发工具模拟器可能和真机表现一致。同时,考虑到微信客户端版本更新可能带来的变化,建议定期检查API的兼容性。 总结步骤应该是:获取胶囊按钮位置信息,计算导航栏高度,自定义导航栏结构,调整样式适配,处理交互冲突,并进行真机测试。同时提供代码示例和配置说明,帮助用户具体实施。</think>### Uniapp微信小程序导航栏与胶囊按钮布局解决方案 在微信小程序开发中,导航栏与胶囊按钮的布局适配需要特别注意以下关键点: **1. 获取胶囊按钮位置** ```javascript const menuButtonInfo = wx.getMenuButtonBoundingClientRect(); const systemInfo = wx.getSystemInfoSync(); // 计算导航栏总高度 const navBarHeight = menuButtonInfo.top + menuButtonInfo.height + 6; // 状态栏高度(单位px) const statusBarHeight = systemInfo.statusBarHeight; ``` **2. 页面结构配置** 在`pages.json`中配置自定义导航栏: ```json { "path": "pages/index/index", "style": { "navigationStyle": "custom", "app-plus": { "titleNView": false } } } ``` **3. 页面布局实现** ```html <template> <!-- 状态栏占位 --> <view :style="{ height: statusBarHeight + 'px' }"></view> <!-- 自定义导航栏 --> <view class="custom-nav" :style="{ height: navBarHeight + 'px' }"> <view class="nav-title">我的小程序</view> </view> </template> ``` **4. 样式适配要点** ```css /* 使用绝对定位避开胶囊按钮区域 */ .custom-nav { position: relative; padding-right: calc(100vw - {{menuButtonInfo.left}}px); } /* 响应式单位转换 */ .nav-title { font-size: 16px; line-height: 32rpx; margin-left: 20rpx; } ``` **5. 多平台适配策略** ```javascript // #ifdef MP-WEIXIN const capsule = wx.getMenuButtonBoundingClientRect(); // #endif // #ifdef H5 // 使用浏览器默认导航栏处理 // #endif ``` **常见问题处理:** 1. **布局错位**:确保使用`rpx`单位,避免固定像素值 2. **点击穿透**:通过`z-index`控制层级关系 3. **iPhone适配**:特别处理`safe-area-inset-top`值 4. **安卓兼容**:注意同厂商的状态栏高度差异 **最佳实践建议:** - 使用`uni.getSystemInfoSync()`获取设备信息 - 开发阶段使用`console.log`输出胶囊按钮坐标 - 发布前需在iOS/Android真机测试 - 定期检查微信开发者工具更新日志[^2]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值