【问题标题】:Checkbox Alert Controller - Add 3 Buttons复选框警报控制器 - 添加 3 个按钮
【发布时间】:2017-09-22 17:27:30
【问题描述】:

我使用 IONIC-2 Beta 版制作了一个应用程序。我正在使用复选框警报控制器,并添加两个按钮“确定”和“取消”,现在我需要在警报控制器中再添加一个按钮。我在下面实现添加一个按钮。

alert.addButton('Cancel');
 alert.addButton({
   text: 'Info',
   role: 'info',
   handler: data => {
     //this.nav.push(DoubleMatPage,{"form_data":this.form_data,"offset":data});
   }
 });
 alert.addButton({
   text: 'Okay',
   handler: data => {
     this.nav.push(DoubleMatPage,{"form_data":this.form_data,"offset":data});
   }
 });

效果很好,但它会像下面一样垂直对齐,

但是,我想把它水平对齐。

如果有人知道解决方案,请帮助我。

谢谢。

【问题讨论】:

    标签: css typescript sass ionic2 ionic3


    【解决方案1】:

    您可以使用一些 css 规则来实现。

    由于使用 flexbox 对齐按钮,我们可以覆盖其中的一些 css 规则,以便能够在模式中包含三个按钮而不是两个。首先,请注意我在警报中添加了一个自定义 css 类 (custom-alert),因此我们的 css 规则将影响此警报

    let alert = this.alertCtrl.create({
          cssClass: 'custom-alert',
          ...
    });
    

    然后:

    .custom-alert button.alert-button-group {
        width: 33.33%;
    }
    
    .custom-alert .alert-button-group .button-inner {
        -webkit-box-pack: center;
        -webkit-justify-content: center;
        -ms-flex-pack: center;
        justify-content: center;
    }
    
    .custom-alert .alert-button-group-vertical {
        -webkit-flex-direction: row;
        -ms-flex-direction: row;
        flex-direction: row ;
    }
    

    魔术是通过将 width 设置为 33.33% 以允许按钮适合同一行,然后将 flex-direction: row 添加到容器而不是 column 值来完成的默认(这就是为什么在您的屏幕截图中按钮显示在列中)。

    【讨论】:

    • 是的,它工作正常。我的代码工作正常。现在。太感谢了。你是天才。:)
    • 很高兴我能帮上忙! Ionic 的人是天才,我只是在享受他们所创造的东西:)
    • 是的,谢谢兄弟。请你帮我This Question
    • 好的,让我看看并运行一些测试
    【解决方案2】:

    请看下面的代码

    presentConfirm() {
      let alert = this.alertCtrl.create({
        title: 'Confirm purchase',
        message: 'Do you want to buy this book?',
        buttons: [
          {
            text: 'Cancel',
            role: 'cancel',
            handler: () => {
              console.log('Cancel clicked');
            }
          },
          {
            text: 'Buy',
            handler: () => {
              console.log('Buy clicked');
            }
          },
          {
            text: 'get',
            handler: () => {
              console.log('Buy clicked');
            }
          }
        ]
      });
      alert.present();
    }
    

    要获得一个水平按钮,我们需要一个按钮内的所有按钮 数组

    但在您的情况下,您有 3 个不同的数组。

    有关 AlerControll 的更多信息,请查看link

    【讨论】:

    • 首先感谢您回答我的问题。但仍然遇到这个问题。我想要水平的。如果我使用 2 个按钮,它工作正常,但超过 2 个按钮则不会按我想要的方式显示。
    • 我实现了这个`let alert = Alert.create({buttons: [ { text: 'Cancel', role: 'cancel', handler: () => { console.log('Cancel clicked '); } }, { text: 'Buy', handler: () => { console.log('Buy clicked'); } }, { text: 'Info', handler: () => { console.log ('点击购买'); } } ]});`
    • 这是由于空间问题而发生的问题,我强烈建议您不要使用超过 2 个按钮
    • 在我的项目中是必需的。你还有其他选择吗?
    • 尝试使用网格使用 ion-rowion-col 并减小按钮的大小以达到技巧
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2017-01-06
    • 1970-01-01
    • 2023-03-23
    • 2017-02-06
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多