uniapp兼容iPhoneX头部状态栏(刘海屏)和底部小横条

(一)兼容 iphoneX 头部状态栏

  • 使用uni.getSystemInfo()异步获取系统信息
  • statusBarHeight手机状态栏的高度
  • 详细看 官方API介绍

方式一:封装状态栏组件 statusBar.vue

<template>
	<view class="statusBar" :style="{height:(statusBarHeight) + 'px'}"></view>
</template>
<script>
	export default {
        data () {
            return {
                statusBarHeight: ''
            }
        },
        created () {
        	this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
    	}
    }
</script>
<style lang="scss" scoped>
    .statusBar{
        width: 100%;
    }
</style>
  • 将组件放入需要的页面中:

    <template>
    	<view class="mine-container">
             <!-- statusBar 组件应放在需要用到页面的最前面 -->
    		<statusBar></statusBar>
            
    		<view class="header-bg">
    			<image src="@/static/header_bg.png" mode=""></image>
    		</view>
    	</view>
    </template>
    
    <script>
    	export default{
    		data () {
                return {
                    statusBarHeight: ''
                }
    		},
    		onLoad () {
    			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
    			
    		}
    	}
    </script>
    
    <style lang="scss" scoped>
    	.mine-container{
    		width: 100%;
    		.header-bg{
    			width: 100%;
    			image{
    				width: 100%;
    				height: 295rpx;
    			}
    		}
    	}
    </style>
    
  • 效果展示:可以看到顶部白色区域是 statusBar 组件自动识别状态栏的高度,后面的图片会自动往下掉
    在这里插入图片描述

  • 用方式一时需要将图片占满顶部,可以使用绝对定位,再给图片动态绑定行内样式自动高即可

    • 思路:给图片设计稿的高度加上获取到的状态栏高度,让图片高为自动的

    • 注意:

      1. 需要再获取一次设备状态栏高度

      2. 这里的 295 只是我图片的设计稿高度(你要根据自己的写)

      3. statusBarHeight 单位是 px,转 rpx 需要乘以 2

      <template>
      	<view class="mine-container">
      		<statusBar></statusBar>
      		<view  class="header-bg">
      			<image
      			 :style="{height:(295 + statusBarHeight*2) + 'rpx'}"
      			 src="@/static/header_bg.png" mode=""></image>
      		</view>
      		<view class="user-info-container">
      			<view class="avatar">
      				<image src="@/static/avatar.png" mode=""></image>
      			</view>
      			<view>用户名</view>
      		</view>
      	</view>
      </template>
      
      <script>
      	export default{
      		data () {
      			return{
      				statusBarHeight: ''
      			}
      		},
      		onLoad () {
      			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
      		}
      	}
      </script>
      
      <style lang="scss" scoped>
      	.mine-container{
      		width: 100%;
      		.header-bg{
      			width: 100%;
      			height: auto;
      			position: fixed;
      			top: 0;
      			left: 0;
      			z-index: -1;
      			image{
      				width: 100%;
      				// height: 295rpx;
      			}
      		}
      		.user-info-container{
      			width: 100%;
      			height: 200rpx;
      			display: flex;
      			flex-direction: row;
      			align-items: center;
      			padding: 50rpx;
      			margin-top: 25rpx;
      			box-sizing: border-box;
      			color: white;
      			image{
      				width: 150rpx;
      				height: 150rpx;
      				margin-right: 20rpx;
      			}
      		}
      	}
      </style>
      
  • 效果展示:

在这里插入图片描述

方式二:不封装组件

  • 如需要图片占满顶部,给图片动态绑定行内样式,高为设计稿的高度加上获取到的状态栏高度

    <template>
    	<view class="mine-container">
    		<view  class="header-bg">
    			<image
    			 :style="{height:(295 + statusBarHeight*2) + 'rpx'}" 
    			  src="@/static/header_bg.png" mode=""></image>
    		</view>
    	</view>
    </template>
    
    <script>
    	export default{
    		data () {
                return {
                    statusBarHeight: ''
                }
    		},
    		onLoad () {
    			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
    		}
    	}
    </script>
    
    <style lang="scss" scoped>
    	.mine-container{
    		width: 100%;
    		.header-bg{
    			width: 100%;
                height: auto;
    			position: fixed;
    			top: 0;
    			left: 0;
    			z-index: -1;
    			image{
    				width: 100%;
    				// height: 295rpx;
    			}
    		}
    	}
    </style>
    

方式三:使用 Vuex 管理

  • 在根目录中创建结构

    ├── store                      # 全局store管理
    │   └── modules          	   # 模块化
    │   │   └── statusBar.js       # 获取状态栏高度模块
    │   └── index.js          	   # 状态管理初始化
    └── main.js     
    
  • uniapp 中已经安装了 Vuex,首先引入 vuex 【store/index.js】

    import Vue from 'vue'
    import Vuex from 'vuex'
    Vue.use(Vuex)
    import statusBar from from '@/store/modules/statusBar.js'
    const store = new Vuex.Store({
      modules: {
          statusBar
      }
    })
    export default store
    
  • 获取状态栏高度模块 【store/modules/statusBar.js】

    export default {
     state:{
         statusBarHeight: 0,
     },
     mutations: {
         getStatusBarHeight(state) {
             state.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
         }
     }
    }
    
  • 在页面中使用 【pages/mine/mine.vue】

    <template>
    <view class="mine-container">
         <!-- 放置状态栏高度 -->
     	<view :style="{ height: statusBarHeight + 'px' }"></view>
         
         <view  class="header-bg">
       	<image :style="{height:(295 + statusBarHeight*2) + 'rpx'}" src="@/static/header_bg.png" mode=""></image>
       </view>
       <view class="user-info-container">
       	<view class="avatar">
       		<image src="@/static/avatar.png" mode=""></image>
       	</view>
       	<view>用户名</view>
       </view>
     </view>
    </template>
    <script>
     import { mapState, mapMutations } from 'vuex'
    export default{
         computed: {
             ...mapState({
                 statusBarHeight: state => state.statusBar.statusBarHeight
             })
         },
         methods: {
             ...mapMutations([
                 getStatusBarHeight
             ])
         },
         onload: {
             this.getStatusBarHeight()
         }
     }
    </script>
    <style lang="scss" scoped>
    .mine-container{
       width: 100%;
       .header-bg{
       	width: 100%;
       	height: auto;
       	position: fixed;
       	top: 0;
       	left: 0;
       	z-index: -1;
       	image{
       		width: 100%;
       		// height: 295rpx;
       	}
       }
       .user-info-container{
       	width: 100%;
       	height: 200rpx;
       	display: flex;
       	flex-direction: row;
       	align-items: center;
       	padding: 50rpx;
       	margin-top: 25rpx;
       	box-sizing: border-box;
       	color: white;
       	image{
       		width: 150rpx;
       		height: 150rpx;
       		margin-right: 20rpx;
       	}
       }
    }
    </style>
    

完整demo展示:

(二)兼容 iPhoneX 底部横条

  • 推荐用 css 方式
    • 安全区域距离底部边界距离 env() 跟 constant() 需要同时存在,顺序不能换

      <template>
       <view class:"container">
      	</view>
      </template>
      
      <style lang="scss" scoped>
      	.container{
      		width: 100%;
      		height: 100%;
      		padding-bottom: constant(safe-area-inset-bottom); // 兼容 IOS<11.2 
      		padding-bottom: env(safe-area-inset-bottom); // 兼容 IOS>11.2 
       }
      </style>
      
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值