Ionic 4 选项卡:如何创建自定义选项卡

Olu*_*owe 5 ionic4

我正在尝试将我的选项卡设计为看起来像链接中显示的那样。

显示我想要的选项卡外观的图像

我目前有默认的选项卡样式,但我不知道如何调整它以使其看起来像链接图像中显示的那样。来自社区的任何帮助将不胜感激。

har*_*n68 1

这是我的破解,

在 Chrome Pixel 2 XL 模拟器上进行了测试,这很重要,因为我使用的变量在不同的屏幕比例上看起来绝对不同。我个人使用的是 411x823,这是 Pixel 2 XL 手机的屏幕尺寸。您需要通过 CSS 媒体查询自行处理所有必要的屏幕尺寸情况,以获得通用解决方案。

初始屏幕:这就是我开始的屏幕。我从看起来像默认选项卡离子应用程序的东西开始。就像你拥有的一样。因为我们只处理htmlscss就是我将要展示的内容。

启动画面

tabs-page.scss起始文件。

.tabbar {
  justify-content: center;
}

.tab-button {
  max-width: 200px;
}

::ng-deep ion-icon.icon-button {
  background: var(--ion-color-primary-tint)!important;
  color: white;
  border-radius: 50%!important;
}
Run Code Online (Sandbox Code Playgroud)

tabs-page.html起始文件。

<ion-tabs>
  <ion-tab-bar *ngIf="tabSlot === 'bottom'" slot="bottom">

    <ion-tab-button tab="marketplace">
      <ion-icon mode="md" name="home"></ion-icon>
      <ion-label>Marketplace</ion-label>
    </ion-tab-button>

    <ion-tab-button tab="friends">
      <ion-icon name="contacts"></ion-icon>
      <ion-label>Friends</ion-label>
      <ion-badge *ngIf="friendCount > 0" color="danger">{{friendCount}}</ion-badge>
    </ion-tab-button>

    <ion-tab-button (click)="openRequestPage()">
      <ion-icon name="add-circle-outline" class="icon-button"></ion-icon>
      <ion-label>Request</ion-label>
    </ion-tab-button>

    <ion-tab-button tab="notifications">
      <ion-icon name="notifications"></ion-icon>
      <ion-label>Notifications</ion-label>
      <ion-badge *ngIf="notificationCount > 0" color="danger">{{notificationCount}}</ion-badge>
    </ion-tab-button>

    <ion-tab-button tab="settings">
      <ion-icon name="settings"></ion-icon>
      <ion-label>Settings</ion-label>
    </ion-tab-button>

  </ion-tab-bar>
</ion-tabs>
Run Code Online (Sandbox Code Playgroud)

结束屏幕:这就是您粗略地寻找的内容。 结束画面

最终结果:这就是我最终根据开始的代码创建的结果。解决方案涉及使用box-shadowcss 属性来提供“负”border-radius样式。然后您需要做的是将中心按钮放置在稍高于 的位置tab-bar。我使用本教程来指导我。此外,了解box-shadow工作原理也有助于理清具体细节。

tabs-page.scss结束文件。

.tabbar {
  justify-content: center;
}

.tab-button {
  max-width: 200px;
}

::ng-deep ion-icon.icon-button {
  background: var(--ion-color-primary-tint)!important;
  color: white;
  border-radius: 50%!important;
}

:host ::ng-deep .tabs-inner {
  position: unset!important;
  contain: size style!important;
}

.bottom-tab-bar {
  --background: transparent;
  --border: 0;
}

ion-tab-button {
  --background: beige;
}

.button-center {
  --background: transparent!important;
  ion-icon {
    height: 50px;
    width: 50px;
    font-size: 75px;
  }
}

ion-tab-bar {
  &:before {
    box-shadow: 0 387px 0 300px beige;
    position: absolute;
    margin-top: -48px;
    padding: 56px;
    border-radius: 65%;
    content: '';
  }
}

.inner-left-btn {
  border-radius: 0 39% 0 0; // create the curved style on the left side of the center
}

.inner-right-btn {
  border-radius: 39% 0 0 0; // create the curved style on the right side of the center
}

.inner-center-btn {
  position: absolute;
  left: calc(50% - 35px); // position your button in the center
  bottom: 20px; // position your button slightly above the half bezel
  font-size: 70px;
  --background: transparent;
}
Run Code Online (Sandbox Code Playgroud)

tabs-page.html结束文件。

<ion-tabs>
  <ion-icon name="add-circle-outline" class="icon-button inner-center-btn" (click)="openRequestPage()"></ion-icon>

  <ion-tab-bar *ngIf="tabSlot === 'bottom'" slot="bottom" class="bottom-tab-bar">

    <ion-tab-button tab="marketplace">
      <ion-icon mode="md" name="home"></ion-icon>
      <ion-label>Marketplace</ion-label>
    </ion-tab-button>

    <ion-tab-button tab="friends" class="inner-left-btn">
      <ion-icon name="contacts"></ion-icon>
      <ion-label>Friends</ion-label>
      <ion-badge *ngIf="friendCount > 0" color="danger">{{friendCount}}</ion-badge>
    </ion-tab-button>

    <ion-tab-button class="button-center">
      <div></div>
    </ion-tab-button>

    <ion-tab-button tab="notifications" class="inner-right-btn">
      <ion-icon name="notifications"></ion-icon>
      <ion-label>Notifications</ion-label>
      <ion-badge *ngIf="notificationCount > 0" color="danger">{{notificationCount}}</ion-badge>
    </ion-tab-button>

    <ion-tab-button tab="settings">
      <ion-icon name="settings"></ion-icon>
      <ion-label>Settings</ion-label>
    </ion-tab-button>

  </ion-tab-bar>
</ion-tabs>
Run Code Online (Sandbox Code Playgroud)