鸿蒙OS ArkUI 沉浸式导航栏动态获取高度 -- 小白篇

鸿蒙OS ArkUI 沉浸式导航栏动态获取高度 -- 小白篇

下面展示一些 内联代码片
这是一个 沉浸式的样式工具类,这个没什么可说的,copy过去用就可以了

import window from '@ohos.window';

export class ImmersiveUtils {
  public static immersive(windowStage: window.WindowStage, config: {
    barColor: string,
    contentColor: string,
    navigationColor: string,
    navigatorColor: string
  }) {
    windowStage.getMainWindow()
      .then(win => {
        win.setWindowSystemBarProperties({
          statusBarColor: config.barColor,
          statusBarContentColor: config.contentColor,
          navigationBarColor: config.navigationColor,
          navigationBarContentColor: config.navigatorColor
        });
      })
  }
}

下面展示一些 主体代码
首先在你的EntryAbility或者是你的主入口UIAbility都可以
在onWindowStageCreate里去实现你的沉浸式导航高度获取和存储

  onWindowStageCreate(windowStage: window.WindowStage) {
    // 设置沉浸式代码
    globalThis.windowStage = windowStage;
    // 获取主窗口实例 参考API 9
    windowStage.getMainWindow()
      .then(win => {
        // 设置窗口的布局是否为全屏显示状态
        win.setWindowLayoutFullScreen(true);
        // 获取导航条高度 参考API 9
        // 这个方法为返回的是PX像素,需要使用px2vp方法转换一下 参考API 9
        let navHeight:number = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT).topRect.height
        // 存储高度信息 参考API 9
        PersistentStorage.PersistProp('systemBarHeight', navHeight)
      })
  }

这里是在你的.ets UI页面里去实现的

// 引入工具类方法
import {ImmersiveUtils} from '../Utils/ImmersiveUtils'
@Entry
@Component
struct Login {
// 获取存储的数据信息
// px2vp 这个方法是.ets后缀文件才有的方法 .ts后缀是没有这个方法的
// 为什么使用这个方法上边的代码块有说明,感兴趣的也可以去查查官方的API文档
  @StorageLink('systemBarHeight') systemBarHeight: number = 0;
  build() {
    Column(){
      Text('顶部title')
        .fontSize(18)
        .fontWeight(FontWeight.Bold)
    }.padding({top:px2vp(this.systemBarHeight)}).height('100%').width('100%')
  }
  //设置你的顶部导航栏样式
  onPageShow(){
    ImmersiveUtils.immersive(globalThis.windowStage, {
      barColor: "#00286098",
      contentColor: "#ffffff",
      navigationColor: "#005d99d6",
      navigatorColor: "#ffffff"
    });
  }
}
  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
uni-app是一种跨平台的应用开发框架,可以同时开发小程序、H5和App。沉浸导航栏是指将页面顶部的组件填充到导航栏的区域,使得导航栏和页面顶部的组件融为一体。在uni-app中实现沉浸导航栏可以通过修改页面的style属性来实现。具体步骤如下: 1. 在page.json文件中找到要实现沉浸导航栏的页面,添加以下代码到该页面的style属性中: ``` "style": { "navigationStyle": "custom" } ``` 这样就将该页面的导航栏设置为沉浸导航栏。 2. 通过uni.getSystemInfo方法获取屏幕的尺寸,可以根据需要进行页面布局的调整。 需要注意的是,以上方法适用于uni-app开发的小程序和H5页面。对于微信小程序页面,也可以通过设置导航栏的背景色为透明来实现沉浸导航栏。具体的实现方可以根据具体的需求和开发框架进行调整。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [uni-app实现小程序沉浸导航栏/顶部组件占满导航栏](https://blog.csdn.net/qq_52925863/article/details/127451119)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [uniapp设置导航栏沉浸导航栏以及获取屏幕尺寸](https://blog.csdn.net/EvaY_Yang/article/details/125554733)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值