如何在 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/