How to align ion-title to center in android, as it is align in center in ios by default.
align-title=“center” is not working in ionic 2 <ion-navbar *navbar align-title="center"> <button menuToggle> <ion-icon name="menu"></ion-icon> </button> <ion-title>Title</ion-title> </ion-navbar>
There isn’t really a good way to do this yet. Right now ios is centered by positioning the ion-title element absolutely and then using padding to make space for the buttons, specifically:
@brandyshea@Mobius77 i have the same problem too align-title=“center” is also not working in Ionic 2, my code is:- <ion-navbar *navbar class="navbar_wrapper" hideBackButton align-title="center"> <button menuToggle> <ion-icon name="menu"></ion-icon> </button> <ion-title>San Francisco</ion-title> <ion-buttons end> <button (click)="location()"> <ion-icon name="pin"></ion-icon> </button> </ion-buttons> </ion-navbar>
Using grids solved this issue for me. I use one row and 3 columns. The first two columns’ width is set to 33% each so the last (invisible) column automatically has its width set to 33% also. Then i apply margin-top: 4% globally to all ion-titles via app.scss.
Add the following style to your .scss file (I placed it in app.scss)
// Centering title
// --------------------------------------------------
// a fix for centering the title on all the platforms
ion-header {
.button-md {
box-shadow: none;
}
.toolbar-title {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
}
After that add the following markup to your page
<ion-header>
<!-- use ion-toolbar for a normal toolbar and ion-navbar for navigation -->
<ion-toolbar>
<ion-buttons left>
<!-- left aligned content here -->
</ion-buttons>
<ion-title>
Your title or image
</ion-title>
<ion-buttons right>
<!-- left aligned content here -->
</ion-buttons>
</ion-toolbar>
</ion-header>
Your CSS below does work: centres the title while keeping back button work. But there is a small problem. I use page push/pop animation (sliding in). The title is not centred when the animation starts, and then suddenly jumps to the centre when the animation ends. Is there a fix for this? Basically, I want the title to be centred when the animation starts.
Your CSS works perfectly (centres, back button works, no issue with page animation). The only small problem is that it only works with <ion-navbar mode="ios">, and this makes the back button’s style different from the default Android one.