小程序动态设置导航栏和状态栏

设置导航栏为自定义
  • "navigationStyle": "custom"
    
{

  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "Weixin",
    "navigationBarTextStyle":"black",
    "navigationStyle": "custom"
  },

}

  • index.js
// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    statusHeight: 0,
    menuTop: 0,
    menuHeight: 0
  },
  onLoad() {
    // 获取状态栏的高度
   wx.getSystemInfo({
      success: (result) => {
        console.log(result)
        this.setData({
          statusHeight: result.statusBarHeight
        })
      },
    })
    // 获取胶囊的高度和top值
    let menu = wx.getMenuButtonBoundingClientRect()
    let menuTop = menu.top - this.data.statusHeight;
    this.setData({
      menuTop: menuTop,
      menuHeight: menu.height
    })
  },

})

  • index.wxml
<view class="status-bar1" style="{height: {{statusBarHeight + 'px'}}'}">
 {{statusHeight}}
</view>
<view class="menu" style="height: {{menuHeight + 'px'}};margin-top: {{menuTop +'px'}}">
 首页
</view>
  • index.wxss
.status-bar1 {
  background-color: #ff6600;
}
.menu {
  background-color: #00f;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

在这里插入图片描述

uniapp中使用全局的getApp获取状态栏和导航栏和胶囊的高度
<script>
export default {
  globalData: {
    statusBarHeight: 0, //状态栏高度
    menuTop: 0, //导航栏高度
    menuHeight: 0,
    navHeight: 0,
  },
  methods: {
    initNav() {
      var self = this.globalData;
      this.$nextTick(()=>{
        wx.getSystemInfo({
        	success: res => {
        	   // 状态栏的高度
        		self.statusBarHeight = res.statusBarHeight;
        	}
        });
        const menu = wx.getMenuButtonBoundingClientRect();
        // 胶囊的高度
        self.menuHeight = menu.height;
        // 胶囊距离状态栏的top值
        self.menuTop = menu.top - self.statusBarHeight;
        // 整个导航栏的高度
        self.navHeight = self.menuTop*2 + self.menuHeight
      })
    }
  },
	onLaunch: function() {
		console.log('App Launch');
		//获取胶囊信息
		// 静默登录
    this.initNav()
	},
  
	onShow: function() {
		console.log('App Show');
	},
	onHide: function() {
		console.log('App Hide');
	},


};
</script>



使用

<template>
  <view>
    <!-- 占位符-状态栏 -->
    <view class="ic-status-height" :style="'height: '+app.statusBarHeight+'px;'"></view>
        <!-- 导航栏-->
    <view class="ic-nav"
      :style="{height:app.menuHeight+'px', marginTop:app.menuTop + 'px',marginBottom: app.menuTop + 'px'}">
      <!-- 或者直接设置 导航栏的高度 navHeight -->
      <view class="ic-nav-left">
        <image src="../../static/account/IMicon.png" mode="widthFix"></image>
        <text>{{name}}</text>
      </view>
      <view class="ic-nav-center">
        <text>{{title}}</text>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "index-custom-nav",
    props: {
      name: {
        type: String,
        default: "杭州"
      },
      title: {
        type: String,
        default: "ME杭州"
      },
    },
    data() {
      return {
        app: getApp().globalData
      };
    },
    mounted() {
      console.log("this", this.app)
    }
  }
</script>

<style lang="scss" scoped>
  .ic-status-height {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
  }

  .ic-nav {
    display: flex;
    align-items: center;
    padding: 0 20rpx;
    color: #ffffff;
    font-weight: 500;
    position: relative;

    .ic-nav-left {
      position: absolute;
      left: 20rpx;
      top: 50%;
      transform: translateY(-50%);
      font-size: 28rpx;
      display: flex;
      align-items: center;

      image {
        width: 34rpx;
        height: 52rpx;
        margin-right: 12rpx;
      }
    }

    .ic-nav-center {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      font-size: 36rpx;
    }
  }
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxslxskxs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值