当前位置: 移动技术网 > IT编程>网页制作>CSS > 微信小程序自定义导航(兼容各种手机)

微信小程序自定义导航(兼容各种手机)

2018年12月12日  | 移动技术网IT编程  | 我要评论

详细代码请见github,请点击地址,其中有原生小程序的实现,也有wepy版本的实现

了解小程序默认导航

如上图所示,微信导航分为两部分,第一个部分为statusbarheight,刘海屏手机(iphone x,小米8等)会比其他的手机高很多,第二部分为titlebarheight,安卓和ios的高度不同,但是ios高度是一样的,ios高度是一样的,

所以我们要实现一个兼容不同手机的导航必须要根据不同的手机实现statusbarheight和titlebarheight

第一步:全局设置

把app.json中的window中的navigationstyle设置为custom官方文档链接

设置完之后发现导航栏变成了如下图所示,只剩下了右上角胶囊按钮

第二步:确定导航栏两部分的高度

(1)确定第一部分statusbarheight的高度,这部分是手机用来展示时间,信号和手机电量的,我们可以从wx.getsysteminfo从获得

wx.getsysteminfo({
  success: function(res) {
    console.log(res.statusbarheight)
  }
})

(2)第二部分titlebarheight为小程序导航栏的高度,经过我查询无数文档和实践得知,在iphone上titlebarheight=44px,在安卓上titlebarheight = 48px

(3)最后总结一下微信小程序的高度代码为

wx.getsysteminfo({
  success: function(res) {
    let titlebarheight = 0
    if (res.model.indexof('iphone') !== -1) {
      titlebarheight = 44
    } else {
      titlebarheight = 48
    }
    that.setdata({
      statusbarheight: res.statusbarheight,
      titlebarheight: titlebarheight
    });
  },
  failure() {
    that.setdata({
      statusbarheight: 0,
      titlebarheight: 0
    });
  }
})

第三步:编写navigation组件

(1)navigation.js

const app = getapp();
component({
  properties: {
    //小程序页面的标题
    title: {
      type: string,
      default: '默认标题'
    },
    //是否展示返回和主页按钮
    showicon: {
      type: boolean,
      default: true
    }
  },

  data: {
    statusbarheight: 0,
    titlebarheight: 0,
  },

  ready: function () {
    // 因为每个页面都需要用到这连个字段,所以放到全局对象中
    if (app.globaldata && app.globaldata.statusbarheight && app.globaldata.titlebarheight) {
      this.setdata({
        statusbarheight: app.globaldata.statusbarheight,
        titlebarheight: app.globaldata.titlebarheight
      });
    } else {
      let that = this
      wx.getsysteminfo({
        success: function(res) {
          if (!app.globaldata) {
            app.globaldata = {}
          }
          if (res.model.indexof('iphone') !== -1) {
            app.globaldata.titlebarheight = 44
          } else {
            app.globaldata.titlebarheight = 48
          }
          app.globaldata.statusbarheight = res.statusbarheight
          that.setdata({
            statusbarheight: app.globaldata.statusbarheight,
            titlebarheight: app.globaldata.titlebarheight
          });
        },
        failure() {
          that.setdata({
            statusbarheight: 0,
            titlebarheight: 0
          });
        }
      })
    }
  },

  methods: {
    headerback() {
      wx.navigateback({
        delta: 1,
        fail(e) {
          wx.switchtab({
            url: '/pages/main/main'
          })
        }
      })
    },
    headerhome() {
      wx.switchtab({
        url: '/pages/main/main'
      })
    }
  }
})

(2) navigation.wxml

<view style="height:{{titlebarheight}}px;padding-top:{{statusbarheight}}px">
  <view class="header" style="height:{{titlebarheight}}px;padding-top:{{statusbarheight}}px">
    <view wx:if="{{showicon}}" class="title-bar">
      <view class="back" bindtap="headerback"><image src="https://dn-testimage.qbox.me/files/08/6b/086b8e19c7a5aa031dc4df31ca8b53ac2ed32212_644.png"></image></view>
      <view class="line"></view>
      <view class="home" bindtap="headerhome"><image src="https://dn-testimage.qbox.me/files/fc/49/fc4958729bf1937667b68c78f495edeafe30f339_1030.png"></image></view>
    </view>
    <view class="header-title">{{title}}</view>
  </view>
</view>

css就不贴了,有点多,需要的朋友可以去的github上拿 点击地址

第四步:展示效果

                       iphone x展示效果                                                     iphone 7展示效果                                                      

               小米8展示效果

 

 

用我们公司的测试机基本上都试了一遍,基本上都能正常显示,别问我为什么样式和右边这么相似,因为我是叫公司设计给了我样式 

 

解决下拉刷新的问题

 

 图一为默认导航下的下拉刷新,显示正常,图二为自定义导航栏下的下拉刷新,显示异常,中间有一大块空白。

如果解决这个问题,我们自定义一个加载动画,藏在导航底下

(1)把app.json中的window设置为如下,这样加载动画就隐藏了,因为加载动画必须要设置window的backgroundtextstyle=black和backgroundcolor=#f3f3f3才会显示如上图所示

window: {
  "navigationstyle": "custom",
  "backgroundtextstyle": "light",
  "navigationbarbackgroundcolor": "#fff",
  "navigationbartitletext": "icy买手店",
  "navigationbartextstyle": "black"
}

 

(2)修改navigation.wxml

<view style="height:{{titlebarheight}}px;padding-top:{{statusbarheight}}px">
  <view class="header" style="height:{{titlebarheight}}px;padding-top:{{statusbarheight}}px">
    <view wx:if="{{showicon}}" class="title-bar">
      <view class="back" bindtap="headerback"><image src="https://dn-testimage.qbox.me/files/08/6b/086b8e19c7a5aa031dc4df31ca8b53ac2ed32212_644.png"></image></view>
      <view class="line"></view>
      <view class="home" bindtap="headerhome"><image src="https://dn-testimage.qbox.me/files/fc/49/fc4958729bf1937667b68c78f495edeafe30f339_1030.png"></image></view>
    </view>
    <view class="header-title">{{title}}</view>
  </view>
  <view class="loading-wrap"><image class="loading-gif" src="https://dn-testimage.qbox.me/files/e0/35/e03562502eae6d5944bed747b7c21a3c2cce1ff8_1250.gif"></image></view>
</view>

效果如下图,加载动画我可能写的不太好看

 问题:这样做在iphone上是能正常展示的,但是在安卓上还有一点小问题,自定义导航栏的标题和图标有一起滑动

注意点

(1)安卓手机下拉刷新还是会有一点点展示问题

(2)项目所有fixed的元素都需要改,top需要加上导航栏的高度

 目前哪些小程序在用自定义导航栏

我所知道的有 “bilibili”,"票圈长视频",我们公司的小程序也在计划用

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网