HarmonyOS 鸿蒙Next中tab安全区问题

HarmonyOS 鸿蒙Next中tab安全区问题

import { AppStorageV2 } from "@ohos.arkui.StateManagement";
import { TabList } from "../Models/TabModels";
import { Home } from "./Home";
import { Mine } from "./Mine";

// 跳转页面入口函数
@Builder
export function LayoutBuilder() {
  Layout();
}

@Component
struct Layout {
  pathStack: NavPathStack = AppStorageV2.connect(NavPathStack,"navPack",()=> new NavPathStack())!

tabData:TabList[]=[
  {title:'首页',icon:$r('app.media.Home'),cIcon:$r('app.media.cHome'),color:$r('app.color.tab_bottom_background'),cColor:$r('app.color.tab_cBottom_background')},
  {title:'我的',icon:$r('app.media.Mine'),cIcon:$r('app.media.cMine'),color:$r('app.color.tab_bottom_background'),cColor:$r('app.color.tab_cBottom_background')}
]
  @State curentIndex:number=0
  @Builder BuilderTab(item:TabList,index:number){
    Column({space:2}){
      Image(this.curentIndex==index?item.cIcon:item.icon)
        .width(26)
      Text(item.title)
        .font({size:13})
        .fontColor(this.curentIndex==index?item.cColor:item.color)
    }
    .width('100%')
    .height(50)
    .justifyContent(FlexAlign.Center)
  }


  @Styles TabBottom(){
    .backgroundColor('#E6E6FA')
    .expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])
  }

  build() {
    NavDestination() {
      Tabs({barPosition:BarPosition.End}){
        TabContent(){
          Home()
        }
        .tabBar(this.BuilderTab(this.tabData[0],0))
        .TabBottom()


        TabContent(){
          Mine()
        }
        .tabBar(this.BuilderTab(this.tabData[1],1))
        .TabBottom()

      }
      .onChange(index=>{
        this.curentIndex=index
      })
     //.expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])
    }
    .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack;
    })
  }
}

为啥设置TabContent()设置expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])没有生效呢,我这样写对吗


更多关于HarmonyOS 鸿蒙Next中tab安全区问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

TabContent组件的安全区扩展需配合沉浸式状态栏设置,楼主没有启用沉浸式会导致顶部安全区扩展失效,另外Tabs与TabContent组件的expandSafeArea作用域存在层级关系,需分别指定不同方向的安全区扩展。

代码修正

// 在Tabs组件中设置底部安全区

Tabs({barPosition: BarPosition.End}) {

  TabContent(){

    Home()

  }

  .tabBar(...)

  .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP]) // 顶部安全区作用于内容区

}

.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM]) // 底部安全区作用于TabBar

.vertical(false)

.backgroundColor('#E6E6FA')

更多关于HarmonyOS 鸿蒙Next中tab安全区问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


背景知识:

使用 expandSafeArea 进行拓展安全区,是值将此背景颜色拓展到安全区内作为安全区的背景色。详情可以查询开发应用沉浸式效果

问题解决:

代码如下:

import { AppStorageV2 } from "@ohos.arkui.StateManagement";
import { TabList } from "../Models/TabModels";
import { Home } from "./Home";
import { Mine } from "./Mine";

// 跳转页面入口函数
@Builder
export function LayoutBuilder() {
  Layout();
}

@Component
struct Layout {
  pathStack: NavPathStack = AppStorageV2.connect(NavPathStack,"navPack",()=> new NavPathStack())!

tabData:TabList[]=[
  {title:'首页',icon:$r('app.media.Home'),cIcon:$r('app.media.cHome'),color:$r('app.color.tab_bottom_background'),cColor:$r('app.color.tab_cBottom_background')},
  {title:'我的',icon:$r('app.media.Mine'),cIcon:$r('app.media.cMine'),color:$r('app.color.tab_bottom_background'),cColor:$r('app.color.tab_cBottom_background')}
]
  @State curentIndex:number=0
  @Builder BuilderTab(item:TabList,index:number){
    Column({space:2}){
      Image(this.curentIndex==index?item.cIcon:item.icon)
        .width(26)
      Text(item.title)
        .font({size:13})
        .fontColor(this.curentIndex==index?item.cColor:item.color)
    }
    .width('100%')
    .height(50)
    .justifyContent(FlexAlign.Center)
  }

  build() {
    NavDestination() {
      Tabs({barPosition:BarPosition.End}){
        TabContent(){
          Home()
        }
        .tabBar(this.BuilderTab(this.tabData[0],0))
        .backgroundColor(Color.Yellow)


        TabContent(){
          Mine()
        }
        .tabBar(this.BuilderTab(this.tabData[1],1))
        .backgroundColor(Color.Yellow)

      }
      .onChange(index=>{
        this.curentIndex=index
      })
      .backgroundColor(Color.Blue)
     .expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])
    }
    .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack;
    })
  }

真机调试:

cke_17595.png

1.将安全区扩展逻辑提升至Tabs的父容器,确保覆盖整个布局层级

Tabs({ barPosition: BarPosition.End }) {
  // TabContent内容
}
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]) // 作用于Tabs外层
.backgroundColor('#E6E6FA')

2.移除冗余样式设置

直接通过Tabs属性设置背景色与安全区,替代@Styles装饰器

Tabs({ barPosition: BarPosition.End }) {
  TabContent(){ /*...*/ }.tabBar(...)
  TabContent(){ /*...*/ }.tabBar(...)
}
.backgroundColor('#E6E6FA')
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])

cke_318.png

这个是效果图

鸿蒙Next中Tab安全区问题通常指Tab组件在屏幕底部时与设备安全区域(如刘海、圆角或手势导航条)的适配。该问题可通过使用安全区域组件(如SafeArea)或设置合适的padding/margin值来解决,确保内容不被遮挡。具体实现需参考鸿蒙官方UI组件文档中的安全区适配方案。

在TabContent上直接使用expandSafeArea可能不会生效,因为安全区设置通常需要作用于容器组件。建议将expandSafeArea移至Tabs容器或外层布局:

Tabs({ barPosition: BarPosition.End }) {
  // TabContent内容
}
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])

或者在外层NavDestination设置:

NavDestination() {
  // Tabs内容
}
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])

注意底部标签栏通常只需要设置BOTTOM安全区,同时检查设备屏幕类型和系统版本是否支持该API。

回到顶部