Skip to content
TheChatArchiveThe Chat Archive
Perplexitysonar-reasoning-pro-high· PerplexityHugging Face LMArena Search Arena 24K

在uniapp中,有哪些内置的手机高度变量?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4o-search-preview-high’s answer.
  1. User

    在uniapp中,有哪些内置的手机高度变量?

  2. Perplexity Assistant

    在uni-app中,提供了几个内置的CSS变量来处理跨平台开发中不同设备高度的适配问题。这些变量让开发者能够更好地处理各平台的高度差异,尤其是在需要自定义导航栏和适配底部TabBar时特别有用。

    内置高度CSS变量

    uni-app提供的主要高度相关CSS变量有:

    --status-bar-height (状态栏高度)

    • 微信小程序环境:固定为25px
    • App端:手机实际状态栏高度
    • H5端:0

    --window-top (内容区域距离顶部的距离)

    • App端:0
    • 小程序:0
    • H5端:NavigationBar的高度(44px)

    --window-bottom (内容区域距离底部的距离)

    • App端:0
    • 小程序:0
    • H5端:TabBar的高度(50px)

    固定高度组件

    uni-app中,以下组件的高度是固定的:

    NavigationBar (导航栏)

    • App端:44px
    • H5端:44px

    TabBar (底部选项卡)

    • App端:HBuilderX 2.3.4之前为56px,2.3.4之后与H5统一为50px(可自定义高度)
    • H5端:50px

    使用示例

    普通页面使用CSS变量

    <template>
    <view>
    <view class="status_bar">
    <!-- 这里是状态栏 -->
    </view>
    <view>状态栏下的文字</view>
    </view>
    </template>
    <style>
    .status_bar {
      height: var(--status-bar-height);
      width: 100%;
    }
    </style>
    

    在TabBar上方固定元素

    <template>
    <view>
    <view class="toTop">
    <!-- 这里可以放一个向上箭头,它距离底部tabbar上浮10px-->
    </view>
    </view>
    </template>
    <style>
    .toTop {
      bottom: calc(var(--window-bottom) + 10px);
    }
    </style>
    

    nvue页面获取状态栏高度

    由于nvue在App端不支持--status-bar-height变量,需要通过API获取:

    <template>
    <view class="content">
    <view :style="{ height: iStatusBarHeight + 'px'}"></view>
    </view>
    </template>
    <script>
    export default {
      data() {
        return {
          iStatusBarHeight: 0,
        };
      },
      onLoad() {
        this.iStatusBarHeight = uni.getSystemInfoSync().statusBarHeight;
      },
    };
    </script>
    

    使用注意事项

    1. 当设置"navigationStyle":"custom"取消原生导航栏后,由于窗体为沉浸式,会占据状态栏位置。此时可使用一个高度为var(--status-bar-height)的view放在页面顶部,避免内容出现在状态栏。

    2. 在H5端,不存在原生导航栏和tabbar,它们是前端div模拟的。如果设置了固定位置的居底view,在小程序和App端会显示在tabbar上方,但在H5端会与tabbar重叠。解决方法是使用--window-bottom变量,确保元素在各端都固定在tabbar上方。

    3. 在uni-app x 4.0之前的版本,如需获取状态栏高度,可使用uni.getWindowInfo()方式获取[4]。

    4. 快速使用这些CSS变量的方法是:在CSS中输入"hei",编辑器会弹出这三个CSS变量供选择(HBuilderX 1.9.6以上支持)[2][11]。

  3. User

    状态栏是指什么?是手机电池那一栏吗?

  4. Perplexity Assistant

    状态栏是指手机屏幕最顶部的那一行显示区域,不仅仅是显示电池电量的部分,而是整个顶部信息条。

    状态栏的定义与位置

    状态栏是手机屏幕顶部的一条横向区域,用于持续向用户显示重要信息[1][4]。它位于手机最顶端,是用户获取手机基本状态信息的重要界面元素[16]。在iPhone X及更新机型上,状态栏分布在"刘海"两侧;在传统带Home键的手机上,状态栏则是屏幕顶部的完整一行[7]。

    状态栏显示的信息

    状态栏包含多种关键信息,而不仅仅是电池状态:

    1. 时间显示 - 让用户随时了解当前时间[4][11]
    2. 信号强度与网络类型 - 显示手机信号强弱和当前网络类型(如5G、4G、LTE等)[7][14]
    3. 电池信息 - 显示当前电池电量,可能包括百分比和电池样式[14][15]
    4. 连接状态 - 显示WiFi连接、蓝牙、VPN等状态图标[7][14]
    5. 系统状态 - 包括静音模式、勿扰模式、飞行模式等系统设置状态[7][14]
    6. 通知提示 - 显示未读消息、未接电话等通知图标[11]
    7. 特殊功能指示 - 如定位服务、热点共享等功能的状态[7]
    8. 隐私指示灯 - 在新版iOS上,会显示橙色或绿色指示灯表示应用正在使用麦克风或摄像头[7]

    状态栏的功能

    状态栏不仅是信息展示区域,还具有交互功能:

    • 下拉功能 - 在大多数系统中,下拉状态栏可以打开通知中心或控制中心[16]
    • 快速设置 - 通过下拉状态栏可以快速访问常用设置和开关[8][16]
    • 通知管理 - 可以从状态栏查看和管理应用通知[13]

    状态栏的个性化设置

    许多手机系统允许用户自定义状态栏:

    • 选择显示或隐藏电池百分比[12][15]
    • 管理状态栏上显示的图标[8]
    • 调整状态栏样式和显示方式[17]
    • 某些系统甚至允许添加自定义应用的快捷方式到状态栏[5]

    总结来说,状态栏是手机用户界面中非常重要的一部分,它不仅包含电池信息,还包括多种系统状态图标和通知指示,为用户提供了重要的实时信息概览。

  5. User

    那我应该怎么把uni.getSystemInfo获得的statusBarHeight应用到全局scss变量中呢?

  6. Perplexity Assistant

    想要将通过uni.getSystemInfo获取的状态栏高度(statusBarHeight)应用到全局SCSS变量中,需要解决的关键问题是:SCSS变量是在编译时处理的,而uni.getSystemInfo获取的值是在运行时获得的。下面我详细介绍几种可行的解决方案。

    使用CSS变量替代SCSS变量

    这是最推荐的方法,通过先在应用初始化时获取状态栏高度,然后将其设置为全局CSS变量。

    第一步:在App.vue的onLaunch中设置全局CSS变量

    // App.vue
    export default {
      onLaunch() {
        // 获取状态栏高度
        const statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
        // 设置为全局CSS变量
        document.documentElement.style.setProperty('--status-bar-height', `${statusBarHeight}px`);
      }
    }
    

    对于非H5平台,需要修改一下设置方式:

    // App.vue
    export default {
      onLaunch() {
        // 获取状态栏高度
        const sysInfo = uni.getSystemInfoSync();
        const statusBarHeight = sysInfo.statusBarHeight;
        
        // 非H5平台使用uni.addStyle设置全局样式
        // #ifdef APP-PLUS || MP
        uni.addStyle(`:root {--custom-status-bar-height: ${statusBarHeight}px;}`);
        // #endif
        
        // H5平台使用document操作
        // #ifdef H5
        document.documentElement.style.setProperty('--custom-status-bar-height', `${statusBarHeight}px`);
        // #endif
      }
    }
    

    第二步:在全局样式文件中预定义变量

    在uni.scss中定义一个默认值的SCSS变量,以防在CSS变量失效的情况下有备用值:

    // uni.scss
    $status-bar-height: var(--custom-status-bar-height, 0);
    

    第三步:在组件中使用

    <style lang="scss">
    .custom-navbar {
      padding-top: $status-bar-height;
      /* 或直接使用CSS变量 */
      padding-top: var(--custom-status-bar-height);
    }
    </style>
    

    使用uni.addStyle动态添加全局样式

    另一种方法是在获取到statusBarHeight后,动态创建并添加包含该高度的全局样式规则。

    // App.vue
    export default {
      onLaunch() {
        // 获取状态栏高度
        const statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
        
        // 创建包含状态栏高度的样式规则
        const styleContent = `
          .status-bar {
            height: ${statusBarHeight}px;
          }
          .has-status-bar-padding {
            padding-top: ${statusBarHeight}px;
          }
        `;
        
        // 添加全局样式
        uni.addStyle(styleContent);
      }
    }
    

    使用全局JS变量+样式绑定方案

    如果需要在组件中频繁使用这个高度值,可以将其存储在全局变量或Vuex中。

    第一步:创建全局状态存储

    // store/index.js
    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    export default new Vuex.Store({
      state: {
        statusBarHeight: 0
      },
      mutations: {
        setStatusBarHeight(state, height) {
          state.statusBarHeight = height;
        }
      }
    })
    

    第二步:在App.vue中初始化

    // App.vue
    export default {
      onLaunch() {
        const statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
        this.$store.commit('setStatusBarHeight', statusBarHeight);
      }
    }
    

    第三步:在组件中使用

    <template>
    <view :style="{ paddingTop: statusBarHeight + 'px' }" class="custom-navbar">
    <!-- 内容 -->
    </view>
    </template>
    <script>
    export default {
      computed: {
        statusBarHeight() {
          return this.$store.state.statusBarHeight;
        }
      }
    }
    </script>
    

    封装成mixin统一处理

    为了更好地复用这一功能,可以创建一个mixin:

    // mixins/statusBar.js
    export default {
      data() {
        return {
          statusBarHeight: 0
        }
      },
      created() {
        // 获取状态栏高度
        this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
      }
    }
    

    然后在需要使用的组件中引入:

    import statusBarMixin from '@/mixins/statusBar.js';
    
    export default {
      mixins: [statusBarMixin],
      // ...
    }
    

    注意事项

    1. 平台差异:在不同平台上获取的statusBarHeight可能不同,需要做好兼容处理。

    2. 异步问题:确保在使用statusBarHeight之前已经获取到了值,尤其是在页面加载早期。

    3. SCSS局限性:SCSS变量是静态的、编译时的,无法直接接收运行时的值,所以必须通过CSS变量或style绑定来处理动态值。

    4. 性能考虑:尽量在应用启动时一次性获取并设置,避免重复调用getSystemInfo。

    5. 页面加载时机:可能需要在onReady生命周期而不是onLoad中获取,以确保能够正确获取到statusBarHeight。

    通过以上方法,你可以有效地将动态获取的状态栏高度应用到全局样式中,实现统一的页面布局适配。

  7. User

    请基于vue3 setup来举例

  8. Perplexity Assistant

    在Vue3的Composition API中,使用setup语法获取状态栏高度并应用到全局变量有多种实现方式。下面我将基于Vue3的setup语法提供几个详细的示例。

    获取状态栏高度并设置为全局CSS变量

    方案一:在App.vue中初始化

    这是最常用的方法,通过在App.vue的setup函数中初始化状态栏高度并设置为全局CSS变量:

    <script setup="">
    import { onLaunch } from '@dcloudio/uni-app';
    
    onLaunch(() => {
      // 获取状态栏高度
      const { statusBarHeight } = uni.getSystemInfoSync();
      
      // 设置全局CSS变量,处理不同平台
      // #ifdef H5
      document.documentElement.style.setProperty('--custom-status-bar-height', `${statusBarHeight}px`);
      // #endif
      
      // #ifdef APP-PLUS || MP
      uni.addStyle(`:root {--custom-status-bar-height: ${statusBarHeight}px;}`);
      // #endif
      
      console.log('状态栏高度:', statusBarHeight);
    });
    </script>
    <style>
    /* 可以在此预定义变量默认值 */
    :root {
      --custom-status-bar-height: 0px;
    }
    </style>
    

    方案二:使用Vuex/Pinia进行状态管理

    使用Pinia(Vue3推荐的状态管理库)存储和管理状态栏高度:

    // stores/device.js
    import { defineStore } from 'pinia';
    import { ref } from 'vue';
    
    export const useDeviceStore = defineStore('device', () =&gt; {
      const statusBarHeight = ref(0);
      const navBarHeight = ref(0);
      
      // 初始化设备信息
      function initDeviceInfo() {
        const sysInfo = uni.getSystemInfoSync();
        statusBarHeight.value = sysInfo.statusBarHeight;
        
        // 获取胶囊按钮信息(小程序)
        // #ifdef MP
        const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
        navBarHeight.value = menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight.value) * 2;
        // #endif
        
        // #ifdef APP-PLUS || H5
        navBarHeight.value = 44; // 为App和H5设置默认导航栏高度
        // #endif
        
        // 设置CSS变量
        setCSSVariables();
      }
      
      // 设置CSS变量
      function setCSSVariables() {
        const cssText = `
          :root {
            --status-bar-height: ${statusBarHeight.value}px;
            --nav-bar-height: ${navBarHeight.value}px;
            --total-nav-height: ${statusBarHeight.value + navBarHeight.value}px;
          }
        `;
        
        // #ifdef H5
        document.head.appendChild(document.createElement('style')).textContent = cssText;
        // #endif
        
        // #ifdef APP-PLUS || MP
        uni.addStyle(cssText);
        // #endif
      }
      
      return {
        statusBarHeight,
        navBarHeight,
        initDeviceInfo
      };
    });
    

    在App.vue中使用:

    <script setup="">
    import { onLaunch } from '@dcloudio/uni-app';
    import { useDeviceStore } from '@/stores/device';
    
    const deviceStore = useDeviceStore();
    
    onLaunch(() => {
      deviceStore.initDeviceInfo();
    });
    </script>
    

    自定义导航栏组件

    以下是一个完整的自定义导航栏组件示例,使用Vue3 setup语法适配不同平台的状态栏:

    <!-- components/NavBar.vue -->
    <template>
    <view :style="{ height: `${navTotalHeight}px` }" class="nav-wrapper">
    <!-- 状态栏占位 -->
    <view :style="{ height: `${statusBarHeight}px` }" class="status-bar"></view>
    <!-- 导航栏内容区 -->
    <view :style="{ height: `${navContentHeight}px` }" class="nav-content">
    <!-- 左侧区域 -->
    <view class="nav-left">
    <slot name="left">
    <view @click="handleBack" class="back-icon" v-if="showBack">
    <text class="icon"></text>
    </view>
    </slot>
    </view>
    <!-- 标题区域 -->
    <view class="nav-title">
    <slot name="title">
    <text>{{ title }}</text>
    </slot>
    </view>
    <!-- 右侧区域 -->
    <view class="nav-right">
    <slot name="right"></slot>
    </view>
    </view>
    <!-- 底部自定义内容区 -->
    <slot name="bottom"></slot>
    </view>
    <!-- 占位元素,防止内容被导航栏遮挡 -->
    <view :style="{ height: `${navTotalHeight}px` }"></view>
    </template>
    <script setup="">
    import { ref, computed, onMounted } from 'vue';
    
    // Props定义
    const props = defineProps({
      title: {
        type: String,
        default: ''
      },
      showBack: {
        type: Boolean,
        default: true
      },
      backgroundColor: {
        type: String,
        default: '#ffffff'
      }
    });
    
    // 事件
    const emit = defineEmits(['back']);
    
    // 状态
    const statusBarHeight = ref(0);
    const navContentHeight = ref(44); // 默认导航内容区高度
    const menuButtonInfo = ref(null);
    
    // 计算总导航高度
    const navTotalHeight = computed(() => {
      return statusBarHeight.value + navContentHeight.value;
    });
    
    // 返回按钮处理
    const handleBack = () => {
      emit('back');
      
      // 如果页面栈大于1,则返回上一页
      const pages = getCurrentPages();
      if (pages.length > 1) {
        uni.navigateBack();
      }
    };
    
    // 初始化导航高度
    onMounted(() => {
      initNavInfo();
    });
    
    // 获取导航信息
    const initNavInfo = () => {
      const sysInfo = uni.getSystemInfoSync();
      statusBarHeight.value = sysInfo.statusBarHeight;
      
      // 获取胶囊信息(小程序)
      // #ifdef MP
      menuButtonInfo.value = uni.getMenuButtonBoundingClientRect();
      // 计算导航栏高度 = 胶囊高度 + (胶囊顶部距离 - 状态栏高度) * 2
      navContentHeight.value = menuButtonInfo.value.height + 
                              (menuButtonInfo.value.top - statusBarHeight.value) * 2;
      // #endif
      
      // #ifdef APP-PLUS
      navContentHeight.value = 44; // App默认44px
      // #endif
      
      // #ifdef H5
      navContentHeight.value = 44; // H5默认44px
      // #endif
    };
    </script>
    <style lang="scss" scoped="">
    .nav-wrapper {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      z-index: 9999;
      background-color: v-bind(backgroundColor);
    }
    
    .nav-content {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 15px;
      
      .nav-left, .nav-right {
        width: 80px;
        height: 100%;
        display: flex;
        align-items: center;
      }
      
      .nav-left {
        justify-content: flex-start;
        
        .back-icon {
          padding: 6px;
          
          .icon {
            font-size: 20px;
          }
        }
      }
      
      .nav-title {
        flex: 1;
        text-align: center;
        font-size: 16px;
        font-weight: 500;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      
      .nav-right {
        justify-content: flex-end;
      }
    }
    </style>
    

    使用自定义Hooks (组合式函数)

    Vue3的一个强大特性是可以创建自定义组合式函数。下面是一个专门用于获取和管理状态栏高度的hook:

    // hooks/useStatusBar.js
    import { ref, onMounted } from 'vue';
    
    export function useStatusBar() {
      const statusBarHeight = ref(0);
      const navBarHeight = ref(0);
      const menuButtonInfo = ref(null);
      
      // 计算总导航高度
      const totalNavHeight = computed(() =&gt; {
        return statusBarHeight.value + navBarHeight.value;
      });
      
      // 初始化状态栏信息
      function initStatusBarInfo() {
        const sysInfo = uni.getSystemInfoSync();
        statusBarHeight.value = sysInfo.statusBarHeight || 0;
        
        // 针对小程序
        // #ifdef MP
        try {
          menuButtonInfo.value = uni.getMenuButtonBoundingClientRect();
          navBarHeight.value = menuButtonInfo.value.height +
                              (menuButtonInfo.value.top - statusBarHeight.value) * 2;
        } catch (e) {
          navBarHeight.value = 44;
          console.error('获取胶囊按钮信息失败', e);
        }
        // #endif
        
        // 针对App
        // #ifdef APP-PLUS
        navBarHeight.value = 44;
        // #endif
        
        // 针对H5
        // #ifdef H5
        navBarHeight.value = 44;
        // #endif
        
        // 设置全局CSS变量
        setGlobalCSSVariables();
        
        return {
          statusBarHeight: statusBarHeight.value,
          navBarHeight: navBarHeight.value,
          totalHeight: totalNavHeight.value,
          menuButtonInfo: menuButtonInfo.value
        };
      }
      
      // 设置全局CSS变量
      function setGlobalCSSVariables() {
        const cssText = `
          :root {
            --custom-status-bar-height: ${statusBarHeight.value}px;
            --custom-nav-bar-height: ${navBarHeight.value}px;
            --custom-nav-total-height: ${totalNavHeight.value}px;
          }
        `;
        
        // #ifdef H5
        const style = document.createElement('style');
        style.textContent = cssText;
        document.head.appendChild(style);
        // #endif
        
        // #ifdef APP-PLUS || MP
        uni.addStyle(cssText);
        // #endif
      }
      
      onMounted(() =&gt; {
        initStatusBarInfo();
      });
      
      return {
        statusBarHeight,
        navBarHeight,
        totalNavHeight,
        menuButtonInfo,
        initStatusBarInfo
      };
    }
    

    在组件中使用这个hook:

    <template>
    <view class="page-container">
    <!-- 自定义导航栏 -->
    <view :style="{ height: `${totalNavHeight}px` }" class="custom-nav">
    <view :style="{ height: `${statusBarHeight}px` }" class="status-bar"></view>
    <view class="nav-content">自定义导航内容</view>
    </view>
    <!-- 页面内容 -->
    <view :style="{ paddingTop: `${totalNavHeight}px` }" class="content">
          页面内容
        </view>
    </view>
    </template>
    <script setup="">
    import { useStatusBar } from '@/hooks/useStatusBar';
    
    // 使用自定义hook
    const { statusBarHeight, navBarHeight, totalNavHeight } = useStatusBar();
    </script>
    <style lang="scss" scoped="">
    .page-container {
      .custom-nav {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        background-color: #ffffff;
        z-index: 999;
        
        .nav-content {
          height: v-bind(navBarHeight) + 'px';
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
      
      .content {
        /* 其他样式 */
      }
    }
    </style>
    

    这些示例展示了在Vue3的setup语法中获取和应用状态栏高度的多种方式,你可以根据自己的项目需求选择最合适的实现方式。记住,状态栏高度主要用于适配不同设备的顶部安全区域,特别是在自定义导航栏时非常重要。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.