21

I am wondering if this a common problem. My ion-title is not centered in the toolbar on android.

I googled it but I couldn't find anything for ionic 4, what I did found was a pretty good solution in ionic 3.

here it is: https://stackoverflow.com/a/30021395/4983589

I am wondering if somebody know how to do this in ionic4?

Here an image how it looks on android:

enter image description here

Sireini
  • 4,142
  • 12
  • 52
  • 91

4 Answers4

48

Ionic v4 keeps toolbar title on left for android devices and on center for IOS devices.

To use a particular behavior you can use mode="md|ios". md is for android and ios for IOS devices.

Since you want to make the title on center, you can use mode="ios" which will make the toolbar title to be on center for both android and ios devices.

This is my header:

<ion-header>
    <ion-toolbar mode="ios">
        <ion-title>
            Add New Rest
        </ion-title>
        <ion-icon slot="end" name="analytics"></ion-icon>
        <ion-buttons slot="start">
            <ion-buttons slot="start">
                <ion-back-button defaultHref="home"></ion-back-button>
            </ion-buttons>
        </ion-buttons>
    </ion-toolbar>
</ion-header>

Have a look at the screenshot below enter image description here enter image description here

Varun Sukheja
  • 6,170
  • 5
  • 51
  • 93
3

I have tested the followings on my android and the title is centered with other buttons such as back button, etc.

Not too sure if it works on iOS devices
Hopefully this helps:

<ion-toolbar>
    <ion-title style="width: 100%; position: absolute; left: 0; height: 0;">
        Title
    </ion-title>
</ion-toolbar>
samheihey
  • 169
  • 1
  • 3
  • 13
2

Adding mode="ios" does work. To simplify you can add mode="ios" to the main HTML tag in the index.html and this will be applied to the entire app so you don't have to add this to each individual HTML page.

Meraiah
  • 75
  • 5
  • Can you add explanation/reference to how this solves the problem in a better way than the accepted answer? – Chetan Bansal Aug 20 '20 at 08:32
  • **This is an addition to the accepted answer. You can add mode="ios" to each page you want to style individuality but this could involve a lot of repeated code depending on how large of a project you have. If you add this to the main HTML tag in the index.html you only have enter this code once and it will be applied to the entire project and save you from having to add this to each page. – Meraiah Aug 20 '20 at 13:26
  • 5
    This will impact all the components in the entire app.. and not just the ion-toolbar.. everything will take up 'ios' look and feel.. – Chetan Bansal Aug 20 '20 at 17:46
2

If you want to keep android mode, you can do the following in your global .scss file:

ion-title {
  &.md {
    width: 100%;
    position: absolute;
    left: 0;
    height: 0;
    text-align: center;
  }
}

This way on the iOS mode title is already centered, you will only be changing the title alignment on the android

selcuk-sahin
  • 327
  • 2
  • 10