遇到的一个需求是在tabbar上有一个凸起的小图标, 但是微信自带的tabbar是没有这个效果的, 无奈,只能使用自定义tabbar,查找了多方文档后发现大多是原生小程序实现, 关于taro文档的少之又少, 所以记录下来,方便下次查看。

该实现方式是借鉴了官方demo以及一位大佬关于原生小程序自定义tabbar的文章

官方demo传送门:自定义tabbar    文章传送门:点击此处

关于taro实现方式,我放在了github上:https://github.com/puerilexy/tabbarDemo

效果展示:(可以看到下面凸出的智能机器人图标)

第一步: 在app.js中的tabbar字段下指定custom: true,即为启用自定义tabbar

config = {
pages: [
'pages/index/index',
'pages/user/user',
'pages/intellect/intellect'
],
window: {
backgroundColor: '#f4f4f4',
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
navigationBarTitleText: 'WeChat',
navigationBarTextStyle: 'black'
},
tabBar: {
color: '#666',
selectedColor: '#ed6c00',
backgroundColor: '#fafafa',
borderStyle: 'black',
custom: true,
list: [{
pagePath: 'pages/index/index',
iconPath: 'assets/home.png',
selectedIconPath: 'assets/home-active.png',
text: '主页'
}, {
pagePath: 'pages/user/user',
iconPath: 'assets/user.png',
selectedIconPath: 'assets/user-active.png',
text: '我的'
}]
}
}

第二步:在src目录下新建custom-tab-bar文件夹(注意此文件夹和pages文件目录同级)

├── src
├── custom-tab-bar
├── index.js
├── index.scss
├── pages
├── index
├── user
├── intellect

在custom-tab-bar下的index文件中代码如下:

import Taro, {
Component
} from '@tarojs/taro'
import {
CoverView, CoverImage
} from '@tarojs/components'
import Intellect from '../assets/intellect.png'
import './index.scss' class customTabBar extends Component { state = {
selected: 0,
color: '#666',
selectedColor: '#ed6c00',
list: [{
pagePath: '/pages/index/index',
iconPath: '/assets/home.png',
selectedIconPath: '/assets/home-active.png',
text: '主页'
},
{
pagePath: '/pages/user/user',
iconPath: '/assets/user.png',
selectedIconPath: '/assets/user-active.png',
text: '我的'
}
]
} switchTab = (item) => {
const url = item.pagePath
Taro.switchTab({
url
})
} jumpIntellect = () => {
Taro.navigateTo({url: '/pages/intellect/intellect'})
} componentDidMount() {
this.setState({
selected: this.props.ind
})
} // 自定义 tabBar的页面
render() {
return (
<CoverView className='tab-bar'>
<CoverView className='tab-bar-wrap'>
{
this.state.list.map((item, index) => {
return <CoverView className='tab-bar-wrap-item'
onClick={this.switchTab.bind(this, item)}
data-path={item.pagePath}
key={item.text}
>
<CoverImage className='tab-bar-wrap-item-icon' src={this.state.selected === index ? item.selectedIconPath : item.iconPath} />
<CoverView className='tab-bar-wrap-item-btn'
style={{color: this.state.selected === index ? this.state.selectedColor : this.state.color}}
>{item.text}
</CoverView>
</CoverView>
})
}
</CoverView>
<CoverImage className='intellect-icon' src={Intellect} onClick={this.jumpIntellect} />
</CoverView>
)
}
}
export default customTabBar

注意: 这里是我在华为手机测试中出现自定义的tabbar会跟随页面滚动而滚动, 在我选择了cover-view组件后解决了这一问题

另外如果中间的图标跳转为二级页面,则不需要配置在tabbar字段下。

第三步:分别在index合user文件中获取自定义tabbar组件实例,更新选中态(注意:原生小程序中可直接通过this.getTabBar直接获取,在taro中需要通过this.$scope.getTabBar来获取组件实例)

// index.js
componentDidShow () {
if (typeof this.$scope.getTabBar === 'function' && this.$scope.getTabBar()) {
this.$scope.getTabBar().$component.setState({
selected: 0
})
}
} // user.js
componentDidShow () {
if (typeof this.$scope.getTabBar === 'function' && this.$scope.getTabBar()) {
this.$scope.getTabBar().$component.setState({
selected: 1
})
}
}

到此,在微信小程序中自定义凸出图标的tabbar就完成了。

taro中自定义tabbar实现中间图标凸出效果的更多相关文章

  1. iOS 自定义Tabbar实现push动画隐藏效果

    http://wonderffee.github.io/blog/2013/08/07/hide-custom-tab-bar-with-animation-when-push/ 在之前的一篇文章(链 ...

  2. Flutter AppBar 自定义顶部导航按钮 图标、颜色 以及 TabBar 定义顶部 Tab 切换

    Flutter AppBar 自定义顶部按钮图 标.颜色 属性 描述 leading 在标题前面显示的一个控件,在首页通常显示应用 的 logo;在其他界面通常显示为返回按钮 title 标题,通常显 ...

  3. AppBar 自定义顶部导航按钮 图标、颜色 以及 TabBar 定义顶部 Tab 切换

    一.Flutter AppBar 自定义顶部按钮图标.颜色 leading   在标题前面显示的一个控件,在首页通常显示应用的 logo:在其他界面通常显示为返回按钮 title  标题,通常显示为当 ...

  4. IOS中如何自定义web应用的图标

    在iPhone/iPad等苹果移动设备上,可以把网站”添加至主屏幕”,添加时的图标可以在HTML中自定义设置图片. 可以使用apple-touch-icon和apple-touch-icon-prec ...

  5. [iOS微博项目 - 1.6] - 自定义TabBar

    A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...

  6. iOS框架搭建(MVC,自定义TabBar)--微博搭建为例

    项目搭建 1.新建一个微博的项目,去掉屏幕旋转 2.设置屏幕方向-->只有竖向 3.使用代码构建UI,不使用storyboard 4.配置图标AppIcon和LaunchImage 将微博资料的 ...

  7. 自定义tabBar

    ★★★★自定义tabBar★★★★★★★ Demo下载地址:https://github.com/marlonxlj/tabBarCustom.git 前言: 有的时候需求要对tabBar进行自定义的 ...

  8. IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)

    ********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...

  9. iOS开发之功能模块--关于自定义TabBar条

    只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. #import <UIKit/UIKit.h> @interface BikeTabBa ...

随机推荐

  1. robot framework 接口post请求需要加headers

    说明:当你用RF进行post接口测试时候,那么需要加个headers=Content-Type=application/x-www-form-urlencoded,要不然会请求不成功的.

  2. IDEA快捷键/本文仅供自己参考使用如有侵权立删

    好好来学习学习IDEA这款神器,让你的效率飞起!视频来自慕课网 本文转载 更多技巧 代码定位 跳转: 1.IDEA的左侧侧边栏有1:Project.7:Structure和2:Favorities a ...

  3. k8spod的介绍

    yaml介绍 apiVersion: v1 APIserver 的版本 kind: Pod 资源类型 metadata: 元数据定义 name: pod-demo 元数据资源名字 labels: 定义 ...

  4. 2 CSS盒子模型&边框&轮廓&外边距&填充&分组嵌套&尺寸&display与visibility

    盒子模型 Box  Model 所有HTML元素可以看做盒子,CSS模型本质上是一个盒子,封装周围的HTML元素 包括:边距,边框,填充和实际内容 盒子模型允许我们在其他元素和周围元素边框之间的空间放 ...

  5. Java基础 -4.6

    循环嵌套 乘法口诀表 public static void main(String[] args) { for(int x =1;x<10;x++) { for(int y=1;y<=x; ...

  6. Python基础-4 运算符

    运算符 运算符:以1 + 2为例,1和2被称为操作数,"+" 称为运算符. Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 ...

  7. Windows驱动开发-IRP的完成例程

    <Windows驱动开发技术详解 >331页, 在将IRP发送给底层驱动或其他驱动之前,可以对IRP设置一个完成例程,一旦底层驱动将IRP完成后,IRP完成例程立刻被处罚,通过设置完成例程 ...

  8. Java 模拟斗地主

    模拟斗地主 public class M1 { public static void main(String args[]) { DouDiZhu02(); } private static void ...

  9. 异常 日志-<多重catch语句>

    try{ }catch(){ }catch(){ }

  10. Java-用星号打印菱形

    打印如图所示菱形9行9列(提示可以将菱形分成上下两个三角形,分析每行空格数和星号个数的关系) 代码如下: package com.homework.lhh; public class Ex20 { p ...