ionic 上拉菜单(ActionSheet)

Ionic 上拉菜单(Action Sheet)2025 年最新最全中文讲解

(就是那个从屏幕底部“嗖”一下弹上来的操作菜单,超好看!)

1. 一行代码调用(最快上手)

import { ActionSheetController } from '@ionic/angular';

constructor(private actionSheetCtrl: ActionSheetController) {}

async openMenu() {
  const actionSheet = await this.actionSheetCtrl.create({
    header: '选择操作',
    subHeader: '你可以在这里放副标题',
    backdropDismiss: true,        // 点击灰色背景是否关闭
    cssClass: 'my-action-sheet',  // 自定义样式类名
    buttons: [
      {
        text: '删除',
        role: 'destructive',      // 红色高亮(专门给删除用)
        icon: 'trash',
        handler: () => {
          console.log('删除 clicked');
          this.deleteItem();
        }
      },
      {
        text: '分享',
        icon: 'share-social',
        handler: () => {
          console.log('分享 clicked');
        }
      },
      {
        text: '收藏',
        icon: 'heart',
        handler: () => {
          console.log('收藏 clicked');
        }
      },
      {
        text: '取消',
        role: 'cancel',            // 灰色 + 加粗,系统推荐写法
        icon: 'close',
      }
    ]
  });

  await actionSheet.present();
}

页面调用:

<ion-button (click)="openMenu()">打开菜单</ion-button>

2. 所有按钮属性全表(直接抄)

属性说明常用值
text按钮文字(必填)‘删除’
icon左侧图标(可选)‘trash’、’camera’
role特殊角色‘destructive’(红)、’cancel’
handler点击回调(返回 false 可阻止关闭)() => { … }
cssClass给单个按钮加样式‘bold-text’
data自定义数据data: { id: 123 }

3. 超实用 5 个真实场景模板(直接复制)

场景1:长按图片操作(最常用)

{
  text: '保存图片',
  icon: 'download-outline',
  handler: () => this.saveImage()
},
{
  text: '识别图中二维码',
  icon: 'qr-code-outline',
  handler: () => this.scanQR()
},
{
  text: '投诉',
  icon: 'flag-outline',
  handler: () => this.report()
},
{ text: '取消', role: 'cancel' }

场景2:列表项操作

{
  text: '置顶',
  icon: 'arrow-up',
},
{
  text: '标记为未读',
  icon: 'mail-unread-outline',
},
{
  text: '删除会话',
  role: 'destructive',
  icon: 'trash',
},
{ text: '取消', role: 'cancel' }

场景3:带颜色的自定义按钮

/* src/theme/variables.scss 或页面 scss */
.my-action-sheet .red-btn {
  --color: #ff3b30;
  --color-hover: #ff3b30;
}
{
  text: '立即拨打',
  icon: 'call',
  cssClass: 'red-btn',
  handler: () => this.call()
}

4. 全局统一样式(推荐写在 global.scss)

// 让所有 ActionSheet 更大、更圆角
.action-sheet-group {
  border-radius: 20px 20px 0 0 !important;
  overflow: hidden;
}

// 按钮更高一点
.action-sheet-button {
  height: 60px !important;
  font-size: 18px;
}

// 取消按钮更粗
.action-sheet-cancel {
  font-weight: 600 !important;
  height: 70px !important;
}

然后所有页面都自动生效!

5. React 版本(ionic-react)

import { useIonActionSheet } from '@ionic/react';

export const MyComponent: React.FC = () => {
  const [present] = useIonActionSheet();

  const open = () => {
    present({
      header: '更多操作',
      buttons: [
        { text: '删除', role: 'destructive', handler: () => console.log('删') },
        { text: '取消', role: 'cancel' }
      ]
    });
  };

  return <IonButton onClick={open}>打开</IonButton>;
};

6. 官方文档(中文)

https://ionicframework.com/docs/api/action-sheet

总结一句话

全世界最好看的底部上拉菜单就是 Ionic 的 ActionSheet
一行代码 + 几个按钮对象,iOS 是毛玻璃,Android 是圆角卡片,自动适配暗黑模式!

需要我立刻发你一个「2025 最新 Ionic 8 完整 ActionSheet 模板包」(包含 20 个常见场景 + 自定义动画 + 毛玻璃特效)吗?
直接回复「发我 ActionSheet 模板」就行~

文章已创建 2588

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部