惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

星空下的YZY

【生活随笔】终于可以试一试new Bing了 指定日期网页自动变灰 这小小破站(摸鱼)一周年啦 【个人记录参考】Vue3搭建项目 【学习记录】CSS动画 【随笔】将备案接入服务商转为腾讯云 正则表达式 【随笔】模拟退火 【随笔】博弈题基础回顾 live2d-widget(看板娘)修改及部署至服务器 【生活随笔】又是一年226,祝自己生日快乐 【杂记】各类零碎笔记(不定期更新) 友链朋友圈4.X.X前端适配尝试【基于店长Akilar的旧版方案】 使用JDBC操作数据库 Java读写txt文件 Hexo中使用ECharts动态图表尝试 【随笔】通过必应的工具发现别的网站未经允许的转载 【暂不公开】YZY的ACM模板整理-java版 更换Butterfly主题及其美化记录
【学习记录】微信小程序tabBar与自定义navigationBar踩坑记录
星空下的YZY · 2022-10-27 · via 星空下的YZY

前言:

最近在尝试微信小程序,平时把参考书籍中涉及的小项目放在一个小程序中,在涉及tabBar时发现跳转至tabBar页面,导航栏上返回按钮消失,进而开始尝试自定义navigationBar。本文记录我的一些踩坑和最终代码。

因为初学,可能有一些错误或者有比较笨拙的地方,欢迎大佬指出🤣

【转载说明】本文优先发布于我的个人博客www.226yzy.com ,转载请注明出处并注明作者:星空下的YZY。

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0许可协议。

示例:

tabBar在app.json中配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"tabBar": {
"color": "#000000",
"selectedColor": "#000000",
"backgroundColor": "#000000",
"list": [{
"pagePath": "page/component/index",
"text": "组件"
}, {
"pagePath": "page/API/index",
"text": "接口"
}]
}
}

用途:tabBar对象用于配置页面底部的标签栏

标签数量要求:标签数量不得少于2个,最多不得超过5

tabBar配置项参数:

属性 是否必填 标签栏属性
position 在窗口的底部或顶部bottom/top(默认bottom)
color 未选择时,tab文字的颜色
selectedColor 选中时,tab文字的颜色
borderStyle tabBar上边框的颜色,仅支持black/white(默认black)
backgroundColor tab的背景色
list tab的列表,标签数量不得少于2个,最多不得超过5

还有一个custom 字段,在自定义tabBar时配置true/false,详情见自定义 tabBar | 微信开放文档 (qq.com)

list配置项参数:

属性 是否必填 说明
pagePath 页面路径,必须在pages中先定义
iconPath 未选择时,图片路径;当positio为top时,此参数无效;不支持网络图片
selectedIconPath 选择时,图片路径;当positio为top时,此参数无效;不支持网络图片
text tab上按钮文字

tabBar图标

正如上文的list配置项参数中iconPathselectedIconPath的说明,它们不支持网络图片

虽然这里使用的icon大小不大,但如果真想使用网络图片还是有办法的

我们可以使用wx.setTabBarItemwx.setTabBarItem(Object object) | 微信开放文档 (qq.com)

参数:

属性 类型 必填 说明
index number tabBar 的哪一项,从左边算起
text string tab 上的按钮文字
iconPath string 图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,当 postion 为 top 时,此参数无效
selectedIconPath string 选中时的图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px ,当 postion 为 top 时,此参数无效
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

1
2
3
4
5
6
wx.setTabBarItem({
index: 0,
text: 'text',
iconPath: '/path/to/iconPath',
selectedIconPath: '/path/to/selectedIconPath'
})

补充说明:

wx.setTabBarItem写在对应page的js中,并且该page必须是tabBar 页面

跳转到 tabBar 页面

因为我学习微信小程序时,是将参考资料涉及的多个小项目集合起来,通过首页中对应的按钮点击跳转到各自的页面。

但是将tabBar相关参数配置好后,发现原本使用的wx.navigateTo无法用于tabBar页面。

在查阅官方文档后发现可以使用wx.switchTab跳转到 tabBar 页面

wx.switchTab(Object object) | 微信开放文档 (qq.com)

带来的问题:

从官方文档功能描述中可知,通过wx.switchTab跳转到 tabBar 页面时,会关闭其他所有非 tabBar 页面

实际运行时会发现,你将无法返回上一页

自定义navigationBar顶部导航栏

解决上面出现的问题我想到了自定义navigationBar顶部导航栏,

由于刚接触微信小程序没多久,手头的参考书籍对这块内容也没有多少涉及

所以我找到了这一篇作为基础参考微信小程序之自定义顶部导航栏_Rayong有分享的博客-CSDN博客

但很可惜,实际使用其相关代码发现效果不太行🤣

再结合微信小程序自定义navigationBar_小任睡不醒`的博客-CSDN博客_navigation-bar小程序+获取顶部状态栏的高度无烟客来了的博客-CSDN博客小程序获取顶部高度等其它文章的内容,发现那篇代码上有缺失,未涉及app.js中的代码。

我的尝试

以下将记录,我参考上述资料后,最终的相关代码

主要代码是在微信小程序之自定义顶部导航栏_Rayong有分享的博客-CSDN博客这一篇的基础上修改的(因为他的代码我没成功😵‍💫)

首先在components的模板组件文件夹(没有的话自建)中创建顶部导航栏的自定义模板。例如我的components/custom-navigation-barcustom-navigation-bar.js

如何使用自定义组件

在需要使用的页面的json中

1
2
3
4
"usingComponents": {
"navbar": "/components/custom-navigation-bar/custom-navigation-bar"
},
"navigationStyle": "custom"

然后在页面的wxml加上

1
<navbar navTitle="标题" back home></navbar>

wxml部分

1
2
3
4
5
6
7
8
9
10
11
12
<view>
<view class="nav-bar {{isWhite=='true'?'nav-bar-white':''}}" style="height: {{statusBarHeight}}px;background-color:{{navColor}};">
<!-- 标题 -->
<text class="navbar-title" style="top:{{navTitleTop}}px;color:{{navTitleColor}};">{{navTitle}}</text>
<view class="navbarImg" style="top:{{navTitleTop}}px;">
<view wx:if="{{home}}" catchtap="navHome" class="navbar-icon-wrap">
<image src="/images/left.png" class="navbar-icon"></image>
</view>
</view>
</view>
<view class="nav-bar-place" style="height: {{statusBarHeight+44}}px;background-color:{{navColor}};"></view>
</view>

其中/images/left.png请使用您自己的图片资源

有关参数见下文js部分

wxss部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* components/custom-navigation-bar/custom-navigation-bar.wxss */
/*自定义导航栏*/
.nav-bar {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 999999;
}
.nav-bar-place {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 999998;
}

.nav-bar-white {
height: 40rpx;
background-color: #fff;
}

.navbar-title {
position: absolute;
height: 32px;
line-height: 32px;
width: 320rpx;
text-align: center;
font-size: 13px;
left: 28%;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

.navbarImg {
position: absolute;
}

.navbar-icon-wrap {
width: 44px;
height: 32px;
display: flex;
justify-content: center;
align-items: center;
}

.navbar-icon {
width: 25px;
height: 25px;
}

这部分对标题过长的情况也进行了处理,过长部分会由省略号代替

js部分

1.首先是app.js中的,写在onLaunch()

1
2
3
4
5
6
7
wx.getSystemInfo({
success: function (res) {
that.globalData.statusBarHeight = res.statusBarHeight;
}
})
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
that.globalData.menuBottonTop=menuButtonInfo.top

wx.getSystemInfo可以获取状态栏的高度statusBarHeightwx.getSystemInfo(Object object) | 微信开放文档 (qq.com),我采用的导航栏高度设为statusHeight+44px,是从这一篇的评论区发现的自定义navigationBar顶部导航栏,兼容适配所有机型(附完整案例) | 微信开放社区 (qq.com)。经过开发工具中几个机型的测试,确实跟原生导航栏没太大差别。

然后是wx.getMenuButtonBoundingClientRect可以获取菜单按钮(右上角胶囊按钮)的布局位置信息。获取胶囊按钮的top参数是为了适配各机型调整标题的位置。Object wx.getMenuButtonBoundingClientRect() | 微信开放文档 (qq.com)

在app.js中有globalData,里面的参数即将作为全局变量供Component使用

1
2
3
4
globalData: {
statusBarHeight:0,
menuBottonTop:0
}

2.接下来是components中的js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const app = getApp()
Component({



properties: {
navTitle: {
type: String,
value: ''
},
navTitleColor: {
type: String,
value: '#fff'
},
navColor: {
type: String,
value: '#0CC7F7'
},
back: {
type: String,
value: 'true'
},
home: {
type: String,
value: 'true'
},
backPath: {
type: String,
default: ''
},
backDelta: {
type: Number,
default: 1
}
},




data: {
statusBarHeight: app.globalData.statusBarHeight,
navTitleTop: app.globalData.menuBottonTop
},



methods: {

navHome: function () {
wx.navigateTo({
url: '/pages/index/index'
})
}






















}
})

因为我只需要解决跳转tabBar页面后无法返回首页的问题,首页这里删减和保留微信小程序之自定义顶部导航栏_Rayong有分享的博客-CSDN博客的部分代码(注释了的部分我暂未测试是否有效)

自此,实现了自定义navigationBar,如何使用自定义组件请见前文

参考文献

微信开放文档 (qq.com)

微信小程序之自定义顶部导航栏_Rayong有分享的博客-CSDN博客

微信小程序自定义navigationBar_小任睡不醒`的博客-CSDN博客_navigation-bar

小程序+获取顶部状态栏的高度无烟客来了的博客-CSDN博客小程序获取顶部高度

微信小程序开发实战/黑马程序员编著 人民邮电出版社 2019.4(2021.7重印)

最后

暂时就写到这了,如有错误,欢迎大佬留言指出😆

欢迎访问我的小破站https://www.226yzy.com/ 或者GitHub版镜像 https://226yzy.github.io/ 或Gitee版镜像https://yzy226.gitee.io/

我的Github:226YZY (星空下的YZY) (github.com)

【转载说明】本文优先发布于我的个人博客www.226yzy.com ,转载请注明出处并注明作者:星空下的YZY。

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0许可协议。