uniapp 如何自定义导航栏并自适应机型

如今的移动设备有各种不同的屏幕形状,如刘海屏、水滴屏等。这些异形屏会影响页面的布局,尤其是导航栏和底部栏的显示。通过获取安全区域信息,可以确保页面内容不会被异形屏的特殊区域遮挡。

在设计页面顶部导航栏时,可以根据 **safeAreaInsets.top**的值来调整导航栏的位置,使其在不同设备上都能正确显示,避免被刘海区域遮挡。

例如iPhone14Pro max机型,就被挡住了!!!

解决方法如下:

1、在page.json里面配置**"navigationStyle": "custom"** ------导航栏自定义
javascript 复制代码
 {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页",
        //使用自定义导航栏
        "navigationStyle": "custom",
        "navigationBarTextStyle": "white"
      }
 },
2、设计自定义导航栏布局
TypeScript 复制代码
<template>
  <view class="navbar">
    <!-- logo文字 -->
    <view class="logo">
      <image class="logo-image" src="@/static/images/logo.png"></image>
      <text class="logo-text">新鲜 · 亲民 · 快捷</text>
    </view>
    <!-- 搜索条 -->
    <view class="search">
      <text class="icon-search">搜索商品</text>
      <text class="icon-scan"></text>
    </view>
  </view>
</template>
3、适配不同机型

获取各机型信息

javascript 复制代码
<script setup lang="ts">
//获取屏幕边界到安全区域的距离
const { safeAreaInsets } = uni.getSystemInfoSync()
console.log(safeAreaInsets);
</script>

动态设置样式::style="{ paddingTop: safeAreaInsets?.top + 'px' }"

html 复制代码
<template>
  <view class="navbar" :style="{ paddingTop: safeAreaInsets?.top + 'px' }">
    <!-- logo文字 -->
    <view class="logo">
      <image class="logo-image" src="@/static/images/logo.png"></image>
      <text class="logo-text">新鲜 · 亲民 · 快捷</text>
    </view>
    <!-- 搜索条 -->
    <view class="search">
      <text class="icon-search">搜索商品</text>
      <text class="icon-scan"></text>
    </view>
  </view>
</template>

设置完之后就不会挡住啦!!!!

相关推荐
UXbot28 分钟前
UI设计工具推荐合集
前端·人工智能·ui
正在努力Coding37 分钟前
SpringAI - 工具调用
java·spring·ai
攻城狮7号39 分钟前
不懂代码也能造?TRAE+GLM-4.6 手把手教你搭心理咨询智能客服小程序
python·小程序·uni-app·vue·trae·glm我的编程搭子·glm-4.6
敲敲了个代码1 小时前
如何优化批量图片上传?队列机制+分片处理+断点续传三连击!(附源码)
前端·javascript·学习·职场和发展·node.js
@AfeiyuO1 小时前
Vue 引入全局样式scss
前端·vue·scss
光影少年1 小时前
flex布局和grid布局区别,实现两边固定布局中间自适应
前端·css3·web·ai编程
我尽力学1 小时前
面试 总结
java·spring boot·面试
爬台阶的蚂蚁1 小时前
Spring AI Alibaba基础概念
java·spring·ai
计算机学姐1 小时前
基于SpringBoot的演唱会抢票系统
java·spring boot·后端·spring·tomcat·intellij-idea·推荐算法
全栈测试笔记2 小时前
异步函数与异步生成器
linux·服务器·前端·数据库·python