Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic4 - How to align alert buttion in center?

Just a quick fix but how do it center my buttons? I am using "alertCtrl.create"

1

also there does not seem to be some kind of centering in the alert ionic docs page.

https://ionicframework.com/docs/api/alert-controller

 alertCtrl.create ({  
    header: 'Invalid Input',
    message: 'Please enter correct details',
    buttons: ['Okay']
}).then(alertElement => alertElement.present());
    return;
}
like image 983
Button Press Avatar asked Jan 26 '23 11:01

Button Press


2 Answers

use mode md to ios because `mode='ios' by default we have the buttons in center.

alertCtrl.create ({  
    header: 'Invalid Input',
    mode:'ios', //by default you will get the button in center
    message: 'Please enter correct details',
    buttons: ['Okay']
}).then(alertElement => alertElement.present());
    return;
}
like image 187
Mohan Gopi Avatar answered Jan 28 '23 23:01

Mohan Gopi


You can also use a css custom class to center the button. When I tried mode: 'ios', it changed the native look of Android, and showed up like it would be in iOS.

When creating your alert, add a css class property:

alertCtrl.create ({  
    cssClass: 'my-alert', // Custom css class
    header: 'Invalid Input',
    message: 'Please enter correct details',
    buttons: ['Okay']
}).then(alertElement => alertElement.present());
    return;
}

And add my-alert class in your global.scss

.my-alert .alert-wrapper {      
    .alert-button-group {
      justify-content: center;
    }
  }
like image 29
Esdras Vitor Avatar answered Jan 28 '23 23:01

Esdras Vitor