在导航栏组件中,小程序开放了自定义标题栏,只需要在Json文件中设置 "navigationStyle":"custom" ,即可自定义顶部导航栏.
    然后使用 wx.getSystemInfo 获取系统的参数,具体参数参考 点我> ,该参数可以获取系统参数, .result.stausBarHeight 返回的是系统栏的高度
    图片.png
    可以看到,系统栏到胶囊还有一定距离,通过查阅文章可以知道 安卓是8px,IOS是6px ,那么公式可得
    系统栏到胶囊的距离 = 系统栏高度 + 6/8px
    图片.png
    因为胶囊的高度也是动态的,所以我们再动态获取胶囊高度 wx.getMenuButtonBoundingClientRect().height相关链接点我
    图片.png
    粉色是胶囊栏高度,胶囊栏下方还有一点距离,和安卓/IOS一样 也是 6/8的差距,我们可以写一个wx:if来判断系统来设置胶囊栏高度,

    WXML

    1. <!-- 获取胶囊栏高度 -->
    2. <!-- IOS -->
    3. <view wx:if="{{Buer === 0}}" class="view_3" style="height:{{ButtonHeight}}px;margin-top:{{statusBarHeight+6}}px;margin-bottom:6px"></view>
    4. <!-- 安卓 -->
    5. <view wx:else class="view_3" style="height:{{ButtonHeight}}px;margin-top:{{statusBarHeight+8}}px;margin-bottom:8px"></view>

    WXSS

    1. .view_3{
    2. width: 100%;
    3. background: pink;
    4. }

    JS

    1. const app = getApp()
    2. Page({
    3. data: {
    4. statusBarHeight: null,
    5. ButtonHeight: null,
    6. System: null,
    7. Buer: null
    8. },
    9. onLoad: function () {
    10. // star
    11. wx.getSystemInfo({
    12. success: (result) => {
    13. this.setData({ statusBarHeight: result.statusBarHeight })
    14. let height = wx.getMenuButtonBoundingClientRect().height
    15. this.setData({ ButtonHeight: height })
    16. this.setData({ System: result.system })
    17. this.setData({ Buer: this.data.System.indexOf('iOS') })
    18. },
    19. })
    20. // End
    21. },
    22. })