Search code examples
ionic4

Ionic 4 tabs: How to create custom tabs


I am trying to style my tabs to look like what's shown in the link.

Image showing what I want the tab to look like.

I currently have the default tab style and I don't know how to go about adjusting it to look like what is shown in the linked image. Any help from the community will be appreciated.


Solution

  • Here is my crack at it,

    TESTED ON CHROME Pixel 2 XL emulator this is important because the variables I used will absolutely look different on different screen ratios. I personally used 411x823 which is the screen dimension on a Pixel 2 XL phone. You will need to handle all of the necessary screen dimension cases on your own through css media queries to get a general solution.

    Initial Screen: this is what I started with. I started with what looks like the default tabs ionic application. Like what you have. Since we're dealing with only html and scss that is what I will show.

    Starting screen

    tabs-page.scss starting file.

    .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;
    }
    

    tabs-page.html starting file.

    <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>
    

    End Screen: This is what you're looking for in a rough sense. End screen

    End result: this is what I ended up creating from the code I had started with. The solution involves using the box-shadow css attribute to give the "negative" border-radius styling. And then what you need to do is position your center button slightly above your tab-bar. I used this tutorial to guide me. Also looking up how box-shadow works helps with getting the specifics sorted out.

    tabs-page.scss ending file.

    .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;
    }
    

    tabs-page.html ending file.

    <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>