SwiftUI - 列表中的自定义滑动操作

标签 swift list swipe swiftui

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

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

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 modifier自定义列表项的滑动操作。

这也适用于 watchOS 8 和 macOS 12。

这些操作系统将于 2021 年底发布。

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

如果您需要针对旧版本,您最好实现不同的用户界面,例如添加一个切换按钮作为列表项的 subview ,或者 adding a context menu到您的列表项。

关于SwiftUI - 列表中的自定义滑动操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57112426/

相关文章:

java - Arrays.asList() 不能正常工作?

android - 检测用户何时从页面边缘滑动 viewpager

ios - 二元运算符 '<<' 不能应用于两个 'T' 操作数

swift - 在 Swift 中将字符串转换为类

python - 从列表中获取第一个非 None 值

javascript - 在 iPad 中创建水平滚动(使用 Hammer JS 库防止垂直滚动)

javascript - 使用 Bootstrap 滑动列

types - Swift 中的类型 ((Int) -> Int) 是什么意思?

ios - Swift 4 你能用图像注释 PDF 吗

python - 如何打印列表中的元素(非重复)