本文已参与「新人创作礼」活动,一起开启掘金创作之路。
开发自定义导航
取消默认配置
先在pages.json中取消默认导航
{
"path" : "pages/index/index",
"style" : {
"navigationStyle":"custom"
}
}
计算高度
uni.getSystemInfo(OBJECT)动态计算状态栏的高度; 内容使用slot的形式插入。 uni.setNavigationBarTitle(OBJECT) 同微信,uni也提供了api修改导航栏设置 wx.getMenuButtonBoundingClientRect() 获取菜单按钮(右上角胶囊按钮)的布局位置信息。坐标信息以屏幕左上角为原点。
导航栏大概代码:
<template>
<view class="fix-full-page">
<view class="navigation-bar" :style="{height:height}">
<view class="navigation-bar-fixed" :style="{height:height}">
<view class="navigation-bar-textbox" :style="{top:marginTop}">
<view class="button-action" hover-class="button-action-active" @click="back_">
<uni-icons type="left" size="20" color="white"></uni-icons>
</view>
<view v-if="config.showHomeBtn" class="button-action" hover-class="button-action-active" @click="home_">
home
</view>
</view>
<!-- title -->
<view class="navigation-bar-title" :style="'margin-top:'+marginTop+';color:'+config.fontcolor">{{cmsConfig.justForJumpVer.title}}</view>
<view class="navigation-bar-settings"></view>
</view>
</view>
<view class="nav-content" :style="{height: contentHeight}">
<slot />
</view>
</view>
</template>
<script>
import uniIcons from "../uni-icons/uni-icons";
export default {
components: {
uniIcons
},
data() {
return {
cmsConfig: uni.faceidVerifySdkCmsConfig
};
},
computed: {
contentHeight(){
return `calc(100% - ${this.height});`
},
height() {
const { platform, statusBarHeight } = uni.getSystemInfoSync();
let height = statusBarHeight + 4; //ios 24px
if (platform.toLowerCase() == "android") {
height += 4; //android 28px
}
// 胶囊高度 32px 下边框6px height 状态栏高度
return height + 38 + "px";
},
marginTop() {
const { platform, statusBarHeight } = uni.getSystemInfoSync();
let height = statusBarHeight + 4;
if (platform.toLowerCase() == "android") {
height += 4;
}
return height + "px";
}
},
methods: {}
};
</script>
uni提供的来自定义导航栏变量
--status-bar-heightuni提供了css变量系统状态栏高度来自定义导航栏,方便不用计算。此变量在微信小程序环境为固定 25px ,在 App 里为手机实际状态栏高度。
使用css变量优化:
<view class="status-contents">
<view class="status top-view"></view>
<view class="title" style="height: 88px;">
<text>导航栏</text>
</view>
</view>
.status-contents{
height: calc(var(--status-bar-height) + 88px);
}
.top-view{
width: 100%;
position: fixed;
top:0;
}
.status{
height:var(--status-bar-height);
}
.title{
width: 100%;
position: fixed;
top: var(--status-bar-height);
}