SwiftUI - 列表中的自定义滑动操作 [英] SwiftUI - Custom Swipe Actions In List

查看:25
本文介绍了SwiftUI - 列表中的自定义滑动操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 SwiftUI 中使用自定义滑动操作?

How can I use custom Swipe Actions in SwiftUI?

我尝试使用 UIKit 框架来让它们在 SwiftUI 中工作.但这对我不起作用.

I tried to use the UIKit Framework to get these working in SwiftUI. But that doesn't work for me.

import SwiftUI
import UIKit



    init() {
        override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let important = importantAction(at: indexPath)
            return UISwipeActionsConfiguration(actions: [important])
        }
        func importantAction(at indexPath: IndexPath) -> UIContextualAction {
            let action = UIContextualAction(style: .normal, title: "Important") { (action, view, completion) in
                print("HI")
            }
            action.backgroundColor = UIColor(hue: 0.0861, saturation: 0.76, brightness: 0.94, alpha: 1.0) /* #f19938 */
            action.image = UIImage(named: "pencil")
            return action
        }
    }






struct TestView: View {

      NavigationView {
               List {
                    ForEach(appointmentsViewModel.appointments.identified(by: .id)) { appointment in Row_Appointments(appointment: appointment)
                }.onDelete(perform: delete)
            }
        }
    }
}

推荐答案

如果您的部署目标是 iOS 15(或更新版本),那么您可以使用 swipeActions 修饰符 自定义列表的滑动操作项.

If your deployment target is iOS 15 (or newer), then you can use the swipeActions modifier to customize the swipe actions of a list item.

这也适用于 watchOS 8 和 macOS 12.

This also applies to watchOS 8 and macOS 12.

这些操作系统将于 2021 年末发布.

These operating systems will be released in late 2021.

在 SwiftUI 2021 年末版本之前,不支持 List 项目的自定义滑动操作.

Prior to the late 2021 version of SwiftUI, there is no support for custom swipe actions for List items.

如果您需要针对旧版本,最好实现不同的用户界面,例如添加切换按钮作为列表项的子视图,或者 向您的列表项添加上下文菜单.

If you need to target an older version, you would probably be better off implementing a different user interface, like adding a toggle button as a subview of your list item, or adding a context menu to your list item.

这篇关于SwiftUI - 列表中的自定义滑动操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆