明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

出现的情况

刚开始写法 是用了

    uni.getSystemInfo({
                success:(e)=> {    
                this.statusBarHeight = e.statusBarHeight;
              }
            })

复制代码
<template>
    <view class="main" :style="{marginTop:statusBarHeight+'px'}">
        {{statusBarHeight}}
     <view>测试</view>
    
    </view>
</template>
 
<script>
    export default {
        name:"app-navbar",
        onLoad(){
            uni.getSystemInfo({
                success:(e)=> {    
                this.statusBarHeight = e.statusBarHeight;
              }
            })
                 console.log(this.statusBarHeight,'-')
        },
        data() {
            return {
                statusBarHeight:0,
            };
        },
    }
</script>
 
<style scoped lang="scss">
.main{
    
}
</style>
复制代码

解决问题的效果

 

第一种把那些方法不写在onLoad里面写在mounted里面就可以了

 

复制代码
<template>
    <view class="main" :style="{marginTop:statusBarHeight+'px'}">
        {{statusBarHeight}}
     <view>测试</view>
    
    </view>
</template>
 
<script>
    export default {
        name:"app-navbar",
        mounted(){
            uni.getSystemInfo({
                success:(e)=> {    
                this.statusBarHeight = e.statusBarHeight;
              }
            })
                 console.log(this.statusBarHeight,'-')
        },
        data() {
            return {
                statusBarHeight:0,
            };
        },
    }
</script>
 
<style scoped lang="scss">
.main{
    
}
</style>
复制代码

 

 

第二种是用了css识别法  var(--status-bar-height)

 

复制代码
<template>
    <view class="main" style="margin-top:  var(--status-bar-height);">
    <view>测试</view>
    
    </view>
</template>
 
<script>
    export default {
        name:"app-navbar",
        onLoad(){
 
        },
 
        data() {
            return {
            
            };
        },
        methods:{
    
        }
    }
</script>
 
<style scoped lang="scss">
.main{
    
 
}
</style>
复制代码

 

 

如果需要用到状态栏高度

可以这样获取到

 

复制代码
<template>
    <view id="main" style="margin-top: var(--status-bar-height);">
        <view>测试</view>
        状态栏高度{{statusBarHeight}}
    </view>
</template>
 
<script>
    export default {
        name: "app-navbar",
        mounted() {
            const query = uni.createSelectorQuery().in(this);
            query.select('#main').boundingClientRect(data => {
                console.log("得到布局位置信息" + JSON.stringify(data));
                console.log("节点离页面顶部的距离为" + data.top);
                this.statusBarHeight = data.top
            }).exec();
        },
 
        data() {
            return {
                statusBarHeight: 0,
            };
        },
        methods: {
 
        }
    }
</script>
 
<style scoped lang="scss">
    .main {}
</style>
复制代码

 

 

 

 

 

 

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2021-09-14 IntelliJ IDEA 设置和查看内存使用
2012-09-14 SQL中的ISNULL和IS NULL
2011-09-14 SQLServer 2008 误删除恢复
点击右上角即可分享
微信分享提示