当前位置:   article > 正文

uniapp自定义顶部导航栏_uniapp自定义导航栏custom

uniapp自定义导航栏custom

uniapp自定义顶部导航栏分以下三步 

1.设置 "navigationStyle": "custom":

自定义导航栏需要在page.json文件内设置 "navigationStyle": "custom" 属性,添加后自定义导航栏才生效。

2.创建一个自定义导航栏组件:

uniapp项目的components目录下创建一个名为CustomNavigationBar.vue的组件文件,并在文件中编写以下代码:

  1. <template>
  2. <view class="custom-navigation-bar">
  3. <view class="left" @click="navigateBack">
  4. <image class="back-icon" src="/static/back.png"></image>
  5. </view>
  6. <view class="title">{{ title }}</view>
  7. <view class="right">
  8. <!-- 右侧按钮或其他内容 -->
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. props: {
  15. title: {
  16. type: String,
  17. default: ''
  18. }
  19. },
  20. methods: {
  21. navigateBack() {
  22. uni.navigateBack()
  23. }
  24. }
  25. }
  26. </script>
  27. <style scoped>
  28. .custom-navigation-bar {
  29. display: flex;
  30. align-items: center;
  31. justify-content: space-between;
  32. height: 44px;
  33. padding: 0 15px;
  34. background-color: #ffffff;
  35. border-bottom: 1px solid #e5e5e5;
  36. }
  37. .left {
  38. display: flex;
  39. align-items: center;
  40. justify-content: flex-start;
  41. }
  42. .back-icon {
  43. width: 20px;
  44. height: 20px;
  45. }
  46. .title {
  47. flex: 1;
  48. text-align: center;
  49. font-size: 18px;
  50. font-weight: bold;
  51. color: #333333;
  52. }
  53. .right {
  54. display: flex;
  55. align-items: center;
  56. justify-content: flex-end;
  57. }
  58. </style>

3.在页面中使用自定义导航栏组件:

在需要使用自定义导航栏的页面组件中,引入了自定义导航栏组件CustomNavigationBar,并在页面中使用<custom-navigation-bar>标签来替代原有的导航栏。例如,在pages/home/index.vue文件中,编写以下代码:

  1. <template>
  2. <view>
  3. <custom-navigation-bar title="首页"></custom-navigation-bar>
  4. <!-- 页面内容 -->
  5. </view>
  6. </template>
  7. <script>
  8. import CustomNavigationBar from '@/components/CustomNavigationBar.vue'
  9. export default {
  10. components: {
  11. CustomNavigationBar
  12. }
  13. }
  14. </script>

总之,自定义导航栏更加灵活多变,根据自己需求进行设置,如果您觉得麻烦可以使用官方组件<uni-nav-bar>进行导航栏的定义。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号