You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 16中如何移除上下文菜单里的Paste选项?

彻底移除iOS 16中TableView上下文菜单的Paste选项

方法一:明确指定允许的动作(推荐)

重写canPerformAction时,只返回你需要保留的动作,其他所有动作(包括Paste)都返回false,避免父类逻辑干扰:

override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
    // 仅保留你需要的菜单动作,例如复制
    return action == #selector(UIResponderStandardEditActions.copy(_:))
}

如果不需要任何系统编辑动作,直接返回false即可:

override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
    return false
}

方法二:覆盖父类的Paste动作判断

如果需要保留父类的其他动作,仅移除Paste,可以先调用父类实现,再针对Paste动作强制返回false:

override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
    let allowSuper = super.tableView(tableView, canPerformAction: action, forRowAt: indexPath, withSender: sender)
    // 屏蔽Paste动作
    if action == #selector(UIResponderStandardEditActions.paste(_:)) {
        return false
    }
    return allowSuper
}

方法三:自定义上下文菜单完全替换系统菜单

如果上述方法仍无效,可直接自定义上下文菜单,完全接管菜单内容,不包含Paste选项:

override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { suggestedActions in
        // 添加你需要的自定义菜单动作
        let customAction = UIAction(title: "自定义操作", image: UIImage(systemName: "gear")) { _ in
            // 执行你的业务逻辑
        }
        // 仅返回自定义动作,系统默认的Paste不会出现
        return UIMenu(title: "", children: [customAction])
    }
}

说明:iOS 16中系统可能会自动注入Paste选项到上下文菜单,此时仅通过canPerformAction判断可能被父类逻辑覆盖,因此直接自定义菜单或明确指定允许的动作是更可靠的方案。

内容的提问来源于stack exchange,提问作者Yaroslav Dukal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 08:00:29